Skip to content

Instantly share code, notes, and snippets.

@gawen
Created December 29, 2014 15:08
Show Gist options
  • Save gawen/0d9a1afd2b39e204da71 to your computer and use it in GitHub Desktop.
Save gawen/0d9a1afd2b39e204da71 to your computer and use it in GitHub Desktop.
Singleton in Python
class SingletonClass(object):
def __new__(cls):
if not cls._singleton:
self = super(SingletonClass, cls).__new__(cls)
# Real constructor
self._init()
cls._singleton = self
return cls._singleton
def _init(self):
self.value = 42
# Call singleton
print SingletonClass().value
# > 42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment