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

You can import the singleton class decorator as follows:

from singleton_class_decorator import singleton

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.

from singleton_class_decorator import singleton

@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.1.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.1-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for singleton-class-decorator-1.0.1.tar.gz
Algorithm Hash digest
SHA256 f78ffc8cf570db66d22ad33f0a5d5da8ef872d2e3a040b4c67677f4edc556fd2
MD5 748b9df5c7fc7e0c1dd110949389f577
BLAKE2b-256 9542b872724d3789f1e816178d264689ce4ed52828f49b29b89ab15073be2605

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for singleton_class_decorator-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 44485680c69f91ab9f619ece6e9c29a0f0d2d3286cf7d1a15331d0d18c000dcd
MD5 cbd8e1e419cdefc3aeb637fa61a6d226
BLAKE2b-256 57a01183e68ccc2ecb00ee41d63995ba2f1c187122f44c5c67f25d7e0e2fed4a

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