Skip to main content

Singleton Class Decorator

Project description

coverage_badge

Installation

Through GitHub

  1. Go to a folder where you want to store the repo: cd <path_to_folder>
  2. Clone the repo using the following: git clone git@github.com:Jrmy-rbr/singleton-class-decorator.git
  3. Install using pip: pip install ./singleton-class-decorator

Through PyPI

Run python -m pip install singleton-class-decorator

Singleton Class Decorator

This repo implements a decorator that creates singleton classes. As opposed to some other singleton decorators, the singleton classes created through this decorator are true classes. As a consequence, every operation supported by classes is supported by the singleton classes, like the use of instance, or inheritance for example.

The main purpose of this repo was for me to learn more about Python, and some of its more advanced features. But this decorator comes with its advantages. It is not more powerful than for example this commonly used decorator by Kemaweyan. However, it allows using singleton classes as actual classes, and therefore it might be more intuitive to use in situations where Kemayan's singleton object needs to use some extra attribute. Here is a quick illustration of situations in which our decorator behaves differently.

Quick comparison with Kemayan's decorator

Kemayan's decorator

@singleton
class MyClass:
    @static
    def add(x, y):
        return x+y

# This will raise an AttributeError
MyClass.add(1, 2)

# You need to do this instead. It'll return 3
MyClass.__wrapped__.add(1, 2)
@singleton
class MyClass:
    ...

obj = MyClass()

# this will raise an error
isinstance(obj, MyClass)

# you need to do this instead
isinstance(obj, MyClass.__wrapped__)

This decorator

@singleton
class MyClass:
    @static
    def add(x, y):
        return x+y

# This works fine
MyClass.add(1, 2)
@singleton
class MyClass:
    ...

obj = MyClass()

# this works fine
isinstance(obj, MyClass)

Usage

Simplest use case

Here are some usage examples. To make a singleton class you simply need to use the singleton decorator on your class as follows:

@singleton
class MyClass:
    ...

# MyClass is now a singleton, which we can check
assert MyClass() is MyClass()

As mentioned earlier, you can check the type of an object as with a regular class:

obj = MyClass()

type(obj)
# returns `<class 'singleton_class_decorator.MetaClasses.MyClass'>`

type(obj) == MyClass
# This evaluates to True

isinstance(obj, MyClass)
# This evaluates to True

Enabling Inheritance

The above should work for any class. However, if you try to create a child class from MyClass an error will be raised. This is because by default the singleton disables inheritance.

@singleton
class MyClass:
    ...

# is equivalent to the following

@singleton(is_final=True)
class MyClass:
    ...

As you see, the default value of the keyword argument is_final is True, which disables inheritance. But you can easily change it.

# enable inheritance by setting the ketword argument `is_final` for `False`
@singleton(is_final=False)
class MyClass:
    ...

# This will now work
class ChildClass(MyClass):
    ...

# However the ClidClass is not automatically a singleton
assert ChildClass() is not ChildClass()

# If you want a child class to be a singleton, you need to use the singleton decorator
# Again you may set the `is_final` to `True` or `False` as you whish.
@singleton
class OtherChildClass(MyClass):
    ...

# OtherChildClass is a singleton
assert OtherChildClass() is OtherChildClass()

How Does it work?

Go check my blog post about this decorator here

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

singleton-class-decorator-1.0.0.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

singleton_class_decorator-1.0.0-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file singleton-class-decorator-1.0.0.tar.gz.

File metadata

File hashes

Hashes for singleton-class-decorator-1.0.0.tar.gz
Algorithm Hash digest
SHA256 e9c67d803f62a221a4934d7860d66b17ed7ac440ccbd3f967bdd57d48c5c2152
MD5 929848702d258f69241a541f5fe04e14
BLAKE2b-256 e4429e17cf5d00e1bf506c2c176359ded315d645f6b0c9ea14647aca7597b27a

See more details on using hashes here.

File details

Details for the file singleton_class_decorator-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for singleton_class_decorator-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 45a44346c101a669275bcf7bd7b08ab6777983b403d2046126bb5f2d3bc1139e
MD5 706ab1c6fcddd690f098f76cd989a04b
BLAKE2b-256 718842c31d4317e5545ba98049495132815fcbfe1eb7a21e3a68bca2ab9d5b06

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page