{"id":5480,"library":"singleton-decorator","title":"A Testable Singleton Decorator","description":"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.","status":"maintenance","version":"1.0.0","language":"en","source_language":"en","source_url":"https://github.com/Kemaweyan/singleton_decorator","tags":["singleton","decorator","design-pattern","testing"],"install":[{"cmd":"pip install singleton-decorator","lang":"bash","label":"Install with pip"}],"dependencies":[],"imports":[{"symbol":"singleton","correct":"from singleton_decorator import singleton"}],"quickstart":{"code":"from singleton_decorator import singleton\n\n@singleton\nclass MyClass:\n    def __init__(self, value=0):\n        self.value = value\n        print(f\"MyClass initialized with value: {self.value}\")\n\n    def get_value(self):\n        return self.value\n\n# First instance creates and initializes\nobj1 = MyClass(10)\nprint(f\"Obj1 value: {obj1.get_value()}\")\n\n# Second instance returns the same object, __init__ is NOT called again\nobj2 = MyClass(20) # Arguments are ignored after the first call\nprint(f\"Obj2 value: {obj2.get_value()}\")\nprint(f\"Are obj1 and obj2 the same instance? {obj1 is obj2}\")\n\n# Accessing the original class for testing or static methods\n# Note: Use __wrapped__ for direct class method calls or inspection in tests\nprint(f\"Accessing original class via __wrapped__: {MyClass.__wrapped__.__name__}\")\n","lang":"python","description":"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."},"warnings":[{"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.","message":"The `__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.","severity":"gotcha","affected_versions":"1.0.0"},{"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`.","message":"While `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.","severity":"gotcha","affected_versions":"N/A (General issue with basic singleton decorators)"},{"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.","message":"The `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.","severity":"gotcha","affected_versions":"1.0.0"},{"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.","message":"The 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.","severity":"gotcha","affected_versions":"All versions (inherent to the pattern)"}],"env_vars":null,"last_verified":"2026-04-13T00:00:00.000Z","next_check":"2026-07-12T00:00:00.000Z"}