Created
May 3, 2020 02:09
-
-
Save kirankotari/724b15b62dc71cc37569832a6793ad37 to your computer and use it in GitHub Desktop.
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
class SingleTonPattern: | |
_instance = None | |
def __new__(cls, name, *args, **kwargs): | |
if cls._instance is None: | |
print(f'Creating Instance.') | |
cls._instance = object.__new__(cls, *args, **kwargs) | |
return cls._instance | |
def __init__(self, name, *args, **kwargs): | |
print(f"__init__ method called {name}") | |
def __del__(self): | |
print(f"Deleting an instance") | |
self._instance = None | |
def main(): | |
obj = SingleTonPattern('obj') | |
print(f'created object {obj}') | |
obj1 = SingleTonPattern('obj1') | |
print(f'created object {obj1}') | |
del obj1 | |
obj2 = SingleTonPattern('obj2') | |
print(f'created object {obj2}') | |
if __name__=="__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment