Skip to content

Instantly share code, notes, and snippets.

@dario61081
Created October 23, 2023 14:12
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 dario61081/d48fbebb908b40f03ab3578d2c4c674c to your computer and use it in GitHub Desktop.
Save dario61081/d48fbebb908b40f03ab3578d2c4c674c to your computer and use it in GitHub Desktop.
singleton in python decorator
def singleton(cls):
"""
Convert to singleton
"""
instance = [None]
def wrapper(*arg, **kwargs):
if instance[0] is None:
instance[0] = cls(*arg, **kwargs)
return instance[0]
return wrapper
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment