Python number-like class that remembers arithmetic operations?

前端 未结 5 603
甜味超标
甜味超标 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:42

    You could give sympy, a computer algebra system written in Python, give a try.

    E.g.

    >>> from sympy import Symbol
    >>> x = Symbol('x')
    >>> y = 2 * (x + 2) ** 2 - 8
    >>> y
    2*(x + 2)**2 - 8
    >>> y.subs(x,3)
    42
    >>> y.subs(x,2)
    24
    

提交回复
热议问题