Registry / serialization / singleton-decorator

singleton-decorator

JSON →
library1.0.0pypypi✓ verified 22d ago

The `singleton-decorator` is a Python library (version 1.0.0, last released in 2017) that provides a simple decorator to implement the singleton design pattern for classes. It aims to address common pitfalls of other singleton implementations, specifically making the decorated classes more amenable to unit testing by exposing the original class via a `__wrapped__` attribute. The library is stable but not actively developed.

pip install singleton-decorator
INSTALL
IMPORT
SIG · SINGLETON-DECORATO
S
singleton-decorator
serializationpythonv1.0.0
Install
2.4s avg
Import
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.0.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 19.2MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 2.4s · import 0.000s · 20MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

singleton
from singleton_decorator import singleton

This example demonstrates how to apply the `@singleton` decorator to a class. It shows that only one instance of `MyClass` is ever created, and subsequent calls to the constructor will return the existing instance without re-running `__init__`. It also highlights how to access the original, undecorated class using `__wrapped__` for testing purposes.

from singleton_decorator import singleton @singleton class MyClass: def __init__(self, value=0): self.value = value print(f"MyClass initialized with value: {self.value}") def get_value(self): return self.value # First instance creates and initializes obj1 = MyClass(10) print(f"Obj1 value: {obj1.get_value()}") # Second instance returns the same object, __init__ is NOT called again obj2 = MyClass(20) # Arguments are ignored after the first call print(f"Obj2 value: {obj2.get_value()}") print(f"Are obj1 and obj2 the same instance? {obj1 is obj2}") # Accessing the original class for testing or static methods # Note: Use __wrapped__ for direct class method calls or inspection in tests print(f"Accessing original class via __wrapped__: {MyClass.__wrapped__.__name__}")
Debug
Known issues
gotchaThe `__init__` method of a decorated singleton class is only executed during the *first* instantiation. Subsequent calls to the class constructor (e.g., `MyClass()`) will return the existing singleton instance, but any arguments passed in these later calls will be completely ignored and will not affect the already initialized instance.
fix
Design your singleton's `__init__` to handle initial setup only. If dynamic reconfiguration is needed, provide a separate public method (e.g., `configure(new_value)`) on the singleton instance.
affects: 1.0.0
gotchaWhile `singleton-decorator` is designed to mitigate issues with `isinstance()` checks and direct static method calls by providing `__wrapped__`, many simpler singleton decorator implementations can break these functionalities. This library's explicit use of `__wrapped__` allows access to the original class definition.
fix
For unit testing or to access static/class methods of the original, undecorated class directly, use `YourClass.__wrapped__.your_method(obj)` (as demonstrated in the library's documentation) rather than `YourClass.your_method()`, especially when providing mock objects for `self`.
affects: N/A (General issue with basic singleton decorators)
gotchaThe `singleton-decorator` does not explicitly implement thread-safety mechanisms. In a multi-threaded application, it is possible for multiple threads to concurrently attempt to create the first instance, potentially leading to the creation of more than one singleton object, thus violating the pattern.
fix
If your application operates in a multi-threaded environment and requires a truly thread-safe singleton, you must add explicit synchronization (e.g., using `threading.Lock`) within your class's initialization logic or consider a different thread-safe singleton implementation.
affects: 1.0.0
gotchaThe singleton pattern itself is often considered an anti-pattern due to its potential to introduce global state, tight coupling between components, and challenges in unit testing. While `singleton-decorator` attempts to make testing easier, these inherent drawbacks of the pattern should be carefully considered.
fix
Evaluate whether the singleton pattern is truly the most appropriate design for your specific use case. Alternative patterns like dependency injection or factory methods can often lead to more modular and testable code.
affects: All versions (inherent to the pattern)
Upgrade
Version history
1.0.0latest on PyPI · released Aug 10, 2017
Audit
Dependencies

No dependency data recorded yet.

Agent activity
15 hits · last 30 days
node
10
Amazon
1
Resources
singleton-decorator — pip install singleton-decorator · libregistry