Skip to content

Instantly share code, notes, and snippets.

@antonagestam
Created February 13, 2019 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antonagestam/83bf9c7ccef0ebcf0216a1288d2e8f74 to your computer and use it in GitHub Desktop.
Save antonagestam/83bf9c7ccef0ebcf0216a1288d2e8f74 to your computer and use it in GitHub Desktop.
A class whose instances there may at any point only exist one of ...
import weakref
class Same:
__the = None
def __new__(cls, *args, **kwargs):
if cls.__the is None or cls.__the() is None:
obj = super(Same, cls).__new__(cls)
cls.__the = weakref.ref(obj)
return cls.__the()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment