Skip to main content

asingleton

Run tests Upload release assets

>>> 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__'

Release files for asingleton 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for asingleton 0.3.0
File Size Uploaded
asingleton-0.3.0.tar.gz 2.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for asingleton 0.3.0
File Interpreter ABI Platform
asingleton-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 6.7 kB

Release files / asingleton-0.3.0.tar.gz

Download URL asingleton-0.3.0.tar.gz
Size 2.6 kB
Tags Source
SHA-256 checksum
How to use checksums
31f6ff67973dea8c86f08dadb4fb4ee1ff2554150f89df986eb328345b98b66c
BLAKE2b-256 checksum
How to use checksums
738cf5ae0293cee54ac200d994fc610d0ee0759370ad0af133c32075b70649ea
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.3

Release files / asingleton-0.3.0-py3-none-any.whl

Download URL asingleton-0.3.0-py3-none-any.whl
Size 4.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4088f39da621b380e8473c5993e8fc5937ae5ce1cac38ba4914e712b8f583c3e
BLAKE2b-256 checksum
How to use checksums
cb3b70c76c850ceda91558cc28bf524c1345ae36526c700c2187e53de7ff7fc3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.3

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files

0.2.0

2 release files

0.1.0

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page