Skip to content

Instantly share code, notes, and snippets.

@rrader
Created April 21, 2011 07:29
Show Gist options
  • Save rrader/933917 to your computer and use it in GitHub Desktop.
Save rrader/933917 to your computer and use it in GitHub Desktop.
Синглтон на Python как метакласс
class Singleton(type):
def __init__(cls, name, bases, dic):
super(Singleton,cls).__init__(name,bases,dic)
cls.instance = None
def __call__(cls,*args,**kw):
if cls.instance is None:
cls.instance=super(Singleton,cls).__call__(*args,**kw)
return cls.instance
#Использование
class PomodoroState(object): #singleton
__metaclass__ = Singleton
# дальше описывается как обычный класс
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment