1# eval(expression, [globals[, locals]]
2x = 100
3y = 200 # x and y are global vars
4eval('6 * 7')
5# output : 42
6eval('x + y')
7#output : 300
8
9# we can override global vars x and y as follows
10eval('x + y',{'x':50,'y':25})
11#output: 75 and not 300
12
13'''eval accepts the following expression examples:
14literals : 6 and 7 from above are literals also lists, floats, boolean,strings
15names : my_num = 3 # the my_num is the name :NOTE that assignments are not allowed in eval
16attributes: funtion_name.attribute
17operations: * from above is an operator
18functions: must return a value eval( sum( [8,16,32] ) ) gives: 56, fibonacci(3)
19'''
20
1from math import *
2names = {'square_root': sqrt, 'power': pow}
3print(eval('dir()', names))
4
5# Using square_root in Expression
6print(eval('square_root(9)', names))
1#string in another string
2expr="'2+3'"
3print(eval(expr))
4print(eval(eval(expr)))