Last active
September 21, 2015 22:58
-
-
Save AaronPhalen/bf20b41387aa01c70351 to your computer and use it in GitHub Desktop.
Shows basic usage of 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
# singleton.py illustrates how to use singleton design pattern in python | |
# influenced by Trevor Pain https://www.youtube.com/watch?v=6IV_FYx6MQA | |
# Author: Aaron Phalen | Twitter: @aaron_phalen | Email: [email protected] | |
# In python, a singleton is a design patter which allows once instance of a class to be invoked | |
# or stored in memory. | |
class Singleton(object): | |
_instance = None | |
def __new__(self): | |
if not self._instance: | |
self._instance = super(Singleton, self).__new__(self) | |
return self._instance | |
class Singleton(object): | |
_instance = None | |
def __new__(cls): | |
if not cls._instance: | |
cls._instance = cls.__new__(cls) | |
return cls._instance | |
s1 = Singleton() | |
s2 = Singleton() | |
print s1 is s2 | |
# Output: True | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment