In the same way that you type:
A,D = 1,4
A+D
: 5
eval('A+D')
: 5
you can type:
eval('B+Y',{'B':3,'Y':7})
: 10
or, using compile, boost the efficiency in a loop (15 times faster):
c = compile('bool(x%2)','','eval')
for i in range(5):
print eval(c,{'x':i})
: False, True, False, True, False
No comments:
Post a Comment