ai-python-wolverine/examples/buggy_script_2.py

29 wiersze
609 B
Python
Czysty Zwykły widok Historia

2023-04-12 02:02:39 +00:00
#!/usr/bin/env python3
import fire
"""
Run With: with `wolverine examples/buggy_script_2.py "return_2"`
Purpose: Fix singleton code bug in Python
"""
class SingletonClass(object):
def __new__(cls):
if not hasattr(cls, 'instance'):
cls.instance = super(SingletonClass, cls).__new__(cls)
return cls.instance
def return_2():
"""
Always returns 2
"""
singleton = SingletonClass()
new_singleton = SingletonClass()
singleton.a = 1
new_singleton.a = 2
would_be_2 = (singleton.a + singleton.a)
assert would_be_2 == 2
return would_be_2
2023-04-12 02:02:39 +00:00
if __name__=="__main__":
fire.Fire()