Python number-like class that remembers arithmetic operations?

前端 未结 5 596
甜味超标
甜味超标 2020-12-05 05:46

I\'m wondering if there exists a python module that would allow me to do something like this:

x = MagicNumber()
x.value = 3
y = 2 * (x + 2) ** 2 - 8
print y          


        
5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-05 06:28

    >>> magic = lambda x: eval('2 * (x + 2) ** 2 - 8')
    >>> magic(2)
    24
    >>> magic(3)
    42
    >>> magic = lambda x: eval('x ** 4')
    >>> magic(2)
    16
    >>> magic(3)
    81
    

提交回复
热议问题