Singleton Class Decorator
Project description
Installation
Through GitHub
- Go to a folder where you want to store the repo:
cd <path_to_folder> - Clone the repo using the following:
git clone git@github.com:Jrmy-rbr/singleton-class-decorator.git - 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file singleton-class-decorator-1.0.1.tar.gz.
File metadata
- Download URL: singleton-class-decorator-1.0.1.tar.gz
- Upload date:
- Size: 7.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f78ffc8cf570db66d22ad33f0a5d5da8ef872d2e3a040b4c67677f4edc556fd2
|
|
| MD5 |
748b9df5c7fc7e0c1dd110949389f577
|
|
| BLAKE2b-256 |
9542b872724d3789f1e816178d264689ce4ed52828f49b29b89ab15073be2605
|
File details
Details for the file singleton_class_decorator-1.0.1-py3-none-any.whl.
File metadata
- Download URL: singleton_class_decorator-1.0.1-py3-none-any.whl
- Upload date:
- Size: 7.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
44485680c69f91ab9f619ece6e9c29a0f0d2d3286cf7d1a15331d0d18c000dcd
|
|
| MD5 |
cbd8e1e419cdefc3aeb637fa61a6d226
|
|
| BLAKE2b-256 |
57a01183e68ccc2ecb00ee41d63995ba2f1c187122f44c5c67f25d7e0e2fed4a
|