Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
24
Task/Arithmetic-evaluation/Python/arithmetic-evaluation-2.py
Normal file
24
Task/Arithmetic-evaluation/Python/arithmetic-evaluation-2.py
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
>>> import ast
|
||||
>>>
|
||||
>>> expr="2 * (3 -1) + 2 * 5"
|
||||
>>> node = ast.parse(expr, mode='eval')
|
||||
>>> print(ast.dump(node).replace(',', ',\n'))
|
||||
Expression(body=BinOp(left=BinOp(left=Num(n=2),
|
||||
op=Mult(),
|
||||
right=BinOp(left=Num(n=3),
|
||||
op=Sub(),
|
||||
right=Num(n=1))),
|
||||
op=Add(),
|
||||
right=BinOp(left=Num(n=2),
|
||||
op=Mult(),
|
||||
right=Num(n=5))))
|
||||
>>> code_object = compile(node, filename='<string>', mode='eval')
|
||||
>>> eval(code_object)
|
||||
14
|
||||
>>> # lets modify the AST by changing the 5 to a 6
|
||||
>>> node.body.right.right.n
|
||||
5
|
||||
>>> node.body.right.right.n = 6
|
||||
>>> code_object = compile(node, filename='<string>', mode='eval')
|
||||
>>> eval(code_object)
|
||||
16
|
||||
Loading…
Add table
Add a link
Reference in a new issue