class TestedClass(float):
def mockedNew(cls, *args, **kwds):
print("Mocked new")
self = float.__new__(*args, **kwds)
return self
def deleteNew(cls):
cls.__new__ = cls.mockedNew
def __new__(cls, arg=0.0):
"Convert from inch to meter"
print("Normal new")
return float.__new__(cls, arg*0.0254)
def __init__(self, arg=0.0):
print("Not working")
deleteNew = classmethod(deleteNew)
mockedNew = classmethod(mockedNew)
if __name__ == "__main__":
TestedClass.deleteNew()
print(TestedClass(12))
In mod normal, in exemplul de mai sus, daca nu as apela deleteNew rezultatul ar fi:
- Normal new
- Not working
- 0.3048
- Mocked new
- Not working
- 12
Niciun comentariu:
Trimiteți un comentariu