switch singleton example to have a bug in singleton implementation

pull/9/head
BioBootloader 2023-04-22 17:24:47 -07:00
rodzic 459a9c5ef2
commit bb8c6f8e3e
1 zmienionych plików z 17 dodań i 18 usunięć

Wyświetl plik

@ -2,27 +2,26 @@
import fire import fire
""" """
Run With: with `wolverine examples/buggy_script_2.py "return_2"` Run With: with `python wolverine.py examples/buggy_script_2.py`
Purpose: Fix singleton code bug in Python Purpose: Fix singleton code bug in Python
""" """
class SingletonClass(object): class SingletonClass(object):
def __new__(cls): def __new__(cls):
if not hasattr(cls, 'instance'):
cls.instance = super(SingletonClass, cls).__new__(cls) cls.instance = super(SingletonClass, cls).__new__(cls)
return cls.instance return cls.instance
def return_2(): def check_singleton_works():
""" """
Always returns 2 check that singleton pattern is working
""" """
singleton = SingletonClass() singleton = SingletonClass()
new_singleton = SingletonClass() new_singleton = SingletonClass()
singleton.a = 1 singleton.a = 1
new_singleton.a = 2 new_singleton.a = 2
would_be_2 = (singleton.a + singleton.a) should_be_4 = (singleton.a + new_singleton.a)
assert would_be_2 == 2 assert should_be_4 == 4
return would_be_2
if __name__=="__main__": if __name__=="__main__":
fire.Fire() fire.Fire(check_singleton_works)