Skip to content

Instantly share code, notes, and snippets.

@lucgiffon
Created May 3, 2017 10:24
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 lucgiffon/45fd5731817eb1cb1f347d8940ee2ebe to your computer and use it in GitHub Desktop.
Save lucgiffon/45fd5731817eb1cb1f347d8940ee2ebe to your computer and use it in GitHub Desktop.
singleton
def singleton(cls):
"""
Simple singleton implementation.
Usage:
@singleton
class A:
pass
a = A()
b = A()
# a == b
"""
instance = None
def class_instanciation_or_not(*args, **kwargs):
nonlocal instance
if not instance:
instance = cls(*args, **kwargs)
return instance
return class_instanciation_or_not
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment