Skip to content

Instantly share code, notes, and snippets.

@shelling
Created June 21, 2011 03:05
Show Gist options
  • Save shelling/1037162 to your computer and use it in GitHub Desktop.
Save shelling/1037162 to your computer and use it in GitHub Desktop.
Singleton in Python
#!/usr/bin/env python
class Hello(object):
singleton = None
def __new__(clz):
if not clz.singleton:
clz.singleton = object.__new__(clz)
return clz.singleton
def __init__(self):
self.world = "world"
return None
h = Hello()
h.hello = "hello"
print h.hello
print h
print h.world
x = Hello()
print x.hello
print x
print x.world
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment