Skip to main content

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:

repository issues PyPI

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_decorator1-1.1.0rc1.tar.gz (4.2 MB view details)

Uploaded Source

Built Distribution

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

singleton_decorator1-1.1.0rc1-py3-none-any.whl (17.8 kB view details)

Uploaded Python 3

File details

Details for the file singleton_decorator1-1.1.0rc1.tar.gz.

File metadata

  • Download URL: singleton_decorator1-1.1.0rc1.tar.gz
  • Upload date:
  • Size: 4.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for singleton_decorator1-1.1.0rc1.tar.gz
Algorithm Hash digest
SHA256 92c70347c356bfb6d3dbf8ea7963164cc7e05654ad46d8503df8dc97c42c8465
MD5 c3d5a7b85580a0469ae1152829784de8
BLAKE2b-256 4cdb2069160791324db1ad4190bdb2075091b4f82e5bae80961fb0b55a674cf0

See more details on using hashes here.

File details

Details for the file singleton_decorator1-1.1.0rc1-py3-none-any.whl.

File metadata

File hashes

Hashes for singleton_decorator1-1.1.0rc1-py3-none-any.whl
Algorithm Hash digest
SHA256 f5175f418deabcff16754f88dd8645c54ef2a3c12a02f362e7e9f3df3781f605
MD5 232d79fcab3924c907bc5b9439e46f20
BLAKE2b-256 6e8affd345d93ae97b764c073b1b66476b68ff6ee65be412a9aec365771d744f

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