{"id":27295,"library":"pysingleton","title":"pysingleton","description":"A minimal Python library providing a @singleton decorator to turn classes into singletons. Version 0.2.1, released Oct 2014. It is in maintenance mode with rare updates.","status":"maintenance","version":"0.2.1","language":"python","source_language":"en","source_url":"https://github.com/timofurrer/singleton","tags":["singleton","decorator","design-pattern","python2"],"install":[{"cmd":"pip install pysingleton","lang":"bash","label":"Install with pip"}],"dependencies":[],"imports":[{"note":"The decorator is at module level.","symbol":"singleton","correct":"from pysingleton import singleton"}],"quickstart":{"code":"from pysingleton import singleton\n\n@singleton\nclass MyClass:\n    pass\n\na = MyClass()\nb = MyClass()\nassert a is b\nprint(\"Singleton works\")","lang":"python","description":"Apply @singleton to a class to ensure only one instance exists."},"warnings":[{"fix":"Add a threading lock inside the class's __new__ method or use a thread-safe singleton pattern.","message":"Locking in __init__: The singleton decorator does not protect against concurrent instantiation. In multithreaded environments, __init__ may be called multiple times.","severity":"gotcha","affected_versions":"all"},{"fix":"Avoid subclassing singletons; apply @singleton to each class individually.","message":"Subclassing singletons: Subclassing a singleton class may lead to unexpected behavior. The subclass is not automatically a singleton.","severity":"gotcha","affected_versions":"all"},{"fix":"Use Python 3 compatible libraries like 'singledispatch' or custom metaclass for production code.","message":"Python 2 support: The library was built for Python 2 and may have compatibility issues with Python 3 (e.g., metaclass syntax).","severity":"deprecated","affected_versions":"0.2.1"}],"env_vars":null,"last_verified":"2026-04-27T00:00:00.000Z","next_check":"2026-07-26T00:00:00.000Z","problems":[{"fix":"Run 'pip install pysingleton' and ensure the correct Python interpreter is active.","cause":"Library not installed, or wrong Python environment.","error":"ImportError: No module named pysingleton"},{"fix":"Use @singleton (no parentheses) as it directly accepts the class.","cause":"Forgot to call the decorator; used @singleton instead of @singleton().","error":"TypeError: 'str' object is not callable when using @singleton"},{"fix":"Call MyClass() to create the singleton instance first; then use MyClass()._instance.","cause":"Manually accessing _instance before instantiation.","error":"AttributeError: 'MyClass' object has no attribute '_instance'"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}