singleton(cls, [attr_name,] [disable_name_mangling,] [not_just_this_class,]) is cls
Project description
asingleton
>>> from asingleton import singleton
>>> print(singleton.__doc__)
singleton(cls,
attr_name='instance',
disable_name_mangling=False,
not_just_this_class=False) -> cls
singleton(attr_name='instance',
disable_name_mangling=False,
not_just_this_class=False)(cls) -> cls
>>> @singleton
... class Service:
... pass
...
>>> Service() is Service.instance
True
>>> Service() # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
Traceback (most recent call last):
...
AssertionError: There is already an instance of type <class '...Service'>;
it can be accessed through the class attribute 'Service.instance'.
>>> @singleton('__instance')
... class Service:
... @classmethod
... def get_instance(cls):
... return cls.__instance
...
>>> Service() is Service.get_instance()
True
>>> Service.__instance
Traceback (most recent call last):
...
AttributeError: type object 'Service' has no attribute '__instance'
>>> @singleton(not_just_this_class=True)
... class Service:
... pass
...
>>> class Apache(Service):
... pass
...
>>> Apache() is Apache.instance
True
>>> Apache() # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
Traceback (most recent call last):
...
AssertionError: There is already an instance of type <class '...Apache'>;
it can be accessed through the class attribute 'Apache.instance'.
>>> class Service:
... def __new__(cls, *args, **kwargs):
... """My custom __new__"""
... return super().__new__(cls, *args, **kwargs)
... original_new = __new__
...
>>> singleton(Service) is Service
True
>>> Service.__new__ is Service.original_new
False
>>> Service.__new__.__wrapped__ is Service.original_new
True
>>> Service.__new__.__doc__
'My custom __new__'
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
asingleton-0.3.0.tar.gz
(2.6 kB
view hashes)
Built Distribution
Close
Hashes for asingleton-0.3.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4088f39da621b380e8473c5993e8fc5937ae5ce1cac38ba4914e712b8f583c3e |
|
MD5 | b2fc4abafc0ab9b08e437370ba732f6f |
|
BLAKE2b-256 | cb3b70c76c850ceda91558cc28bf524c1345ae36526c700c2187e53de7ff7fc3 |