Skip to content

Instantly share code, notes, and snippets.

@silviud
Created November 14, 2013 18:31
Show Gist options
  • Save silviud/7471916 to your computer and use it in GitHub Desktop.
Save silviud/7471916 to your computer and use it in GitHub Desktop.
python singleton
class Singleton(object):
"""
Singleton interface:
http://www.python.org/download/releases/2.2.3/descrintro/#__new__
"""
def __new__(cls, *args, **kwds):
it = cls.__dict__.get("__it__")
if it is not None:
return it
cls.__it__ = it = object.__new__(cls)
it.init(*args, **kwds)
return it
def init(self, *args, **kwds):
pass
class Me(Singleton):
_data = []
def init(self, me):
print me
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment