Skip to content

Instantly share code, notes, and snippets.

@songthamtung
Created November 12, 2019 05:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save songthamtung/2f69685191e007ebeba7d63ab8803d6c to your computer and use it in GitHub Desktop.
Save songthamtung/2f69685191e007ebeba7d63ab8803d6c to your computer and use it in GitHub Desktop.
class Ceo:
__instance = None
def getInstance():
if Ceo.__instance == None:
Ceo()
return Ceo.__instance
def __init__(self):
if Ceo.__instance != None:
raise Exception("There can only be one CEO. This class is a singleton!")
else:
Ceo.__instance = self
def createCeoA():
ceo_a = Ceo()
ceo_a = ceo_a.getInstance
print(ceo_a)
def createCeoB():
ceo_b = Ceo()
ceo_b = ceo_b.getInstance
print(ceo_b)
if __name__ == "__main__":
createCeoA() # success
createCeoB() # fail
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment