Skip to content

Instantly share code, notes, and snippets.

@victorusachev
Created April 17, 2020 18:04
Show Gist options
  • Select an option

  • Save victorusachev/51749354494b0c57249fe3cbca3bfd5c to your computer and use it in GitHub Desktop.

Select an option

Save victorusachev/51749354494b0c57249fe3cbca3bfd5c to your computer and use it in GitHub Desktop.
class SingletonMeta(type):
_instance = None
_args = None
_kwargs = None
def __call__(cls, *args, **kwargs):
if not cls._instance:
cls._args = args
cls._kwargs = kwargs
cls._instance = super(SingletonMeta, cls).__call__(*args, **kwargs)
else:
if cls._args != args or cls._kwargs != kwargs:
raise ValueError
return cls._instance
class Singleton(metaclass=SingletonMeta):
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment