Created
November 14, 2013 18:31
-
-
Save silviud/7471916 to your computer and use it in GitHub Desktop.
python singleton
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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