Skip to content

Instantly share code, notes, and snippets.

@gaoconghui
Created April 27, 2018 11:33
Show Gist options
  • Save gaoconghui/e7cfda7cc4265237a67bcb9db9ea974c to your computer and use it in GitHub Desktop.
Save gaoconghui/e7cfda7cc4265237a67bcb9db9ea974c to your computer and use it in GitHub Desktop.
python metaclass实现的单例模式
class Singleton(type):
def __init__(cls, name, bases, dict):
super(Singleton, cls).__init__(name, bases, dict)
cls.instance = None
def __call__(cls, *args, **kw):
if cls.instance is None:
cls.instance = super(Singleton, cls).__call__(*args, **kw)
return cls.instance
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment