-
-
Save Hammer2900/b0ea88ac37ac4a6b994c to your computer and use it in GitHub Desktop.
Singelton
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def singleton(cls): | |
| obj = cls() | |
| # Always return the same object | |
| cls.__new__ = staticmethod(lambda cls: obj) | |
| # Disable __init__ | |
| try: | |
| del cls.__init__ | |
| except AttributeError: | |
| pass | |
| return cls |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment