Created
November 12, 2019 05:33
-
-
Save songthamtung/2f69685191e007ebeba7d63ab8803d6c to your computer and use it in GitHub Desktop.
This file contains 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 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