Singleton decorator which is: thread-safe, copy safe, mypy compliant, not wrapped class
Project description
###Singleton Decorator Library
A decorator based on wiki.python.org Python Decorators Library, namely https://wiki.python.org/moin/PythonDecoratorLibrary#Singleton, with significant improvements.
Features (assuming you decorate a class definition of MyClass)
- Every call of MyClass() results in the same instance, which persists from the first instantiation until the end of execution
- Even if you delete all your references to the instance, it still persists to be returned on the next call of MyClass()
- That instance is only initiallized once (the first time)
- is_instance(MyClass(), MyClass) is True (that is, we do not wrap the class)
- Using copy() or deepcopy() simply gives another reference to the single instance
- The overhead of locking in the threaded version is low enough that you may use it even before adding threading to your class.
- If MyClass is threadsafe, then annotating with @singleton will give a threadsafe singleton (only the safety of creating the first instance requires locking, as all other singleton actions are no-ops or simply return a reference to the single instance)
- If MyClass has locking (either Lock or RLock) in the initialization (new and init) the singleton locking does not deadlock.
- The test suite is included in the src package, so you can tell if I verified correctly
- The singleton decorator and test bench pass black and mypy
- The test suite is run before publishing
This has some fixes (correct calling of old init, correct replacement of init, removal of copy and deepcopy, and threadsafety) from the version on the PythonDecoratorLibrary wiki page.
from singleton_decorator1 import singleton
@singleton
class MyClass
pass
a = MyClass()
b = MyClass()
assert(a is b)
c = deepcopy(b)
assert(a is c)
Note that if MyClass is not threadsafe, then the decorator will not fix that -- it only ensures that the singleton functionality is thread safe.
The library can be built with hatchling:
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
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_decorator1-1.0.2.tar.gz.
File metadata
- Download URL: singleton_decorator1-1.0.2.tar.gz
- Upload date:
- Size: 4.2 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0ff46bc7a33f837aac9bbe57305938d6a772bfdc4d89232087061d04df98e4fc
|
|
| MD5 |
79e5ef6be35ee539fdef18acf2c6549f
|
|
| BLAKE2b-256 |
2484a56826013359a1ec75d1b044258e6edd1227b52756fa0188f0c207285cb7
|
File details
Details for the file singleton_decorator1-1.0.2-py3-none-any.whl.
File metadata
- Download URL: singleton_decorator1-1.0.2-py3-none-any.whl
- Upload date:
- Size: 17.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
35d713a3ccaa943f138e8868b5ba5a7133b5928cebbb8da4686b9bf8b3c5dfd3
|
|
| MD5 |
ceb5f08fe9e027c0e66774048d2386b9
|
|
| BLAKE2b-256 |
262b36679087ef1a177db9291dd8c223137dcb2c59e4c99c05dc31a342d96797
|