开发者

How can I change what python interprets as a integer?

How can I change what python interprets a开发者_C百科s a integer? For example: 94*n would be a valid integer.


Anything is possible when you smell like Old Spice and use Python's language services to generate a AST.


On the off chance that you're not trying to modify Python's grammar, you could use int():

>>> n = 1.2
>>> x = 94*n
>>> type(x)
<type 'float'>
>>> y = int(94*n) # use int()
>>> type(y)
<type 'int'>


You can use int() and float() to convert numeric types. If you want a computer algebra system in Python, then you may be interested in taking a look at sympy which lets you do something like:

from sympy import *

n = Symbol('n')
x = 94*n
print x
print x.subs(n, 5)

If you are trying to write a computer algebra system, I would recommend using Sympy if it meets your needs or contributing to Sympy to enhance it rather than creating a whole new system from scratch.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜