laityc 2019-12-17
计算指定表达式的值。也就是说它要执行的Python代码只能是单个运算表达式(注意eval不支持任意形式的赋值操作),而不能是复杂的代码逻辑,这一点和lambda表达式比较相似。
eval(expression, globals=None, locals=None)
x = 10 def func(): y = 20 a = eval(‘x + y‘) print(‘a: ‘, a) b = eval(‘x + y‘, {‘x‘: 1, ‘y‘: 2}) print(‘b: ‘, b) c = eval(‘x + y‘, {‘x‘: 1, ‘y‘: 2}, {‘y‘: 3, ‘z‘: 4}) print(‘c: ‘, c) d = eval(‘print(x, y)‘) print(‘d: ‘, d) func()
输出结果:
a: 30 b: 3 c: 4 10 20 d: None
对输出结果的解释:
动态执行Python代码。也就是说exec可以执行复杂的Python代码,而不像eval函数那么样只能计算一个表达式的值。
exec(object[, globals[, locals]])
exec函数的返回值永远为None.
需要说明的是在Python 2中exec不是函数,而是一个内置语句(statement),但是Python 2中有一个execfile()函数。可以理解为Python 3把exec这个statement和execfile()函数的功能够整合到一个新的exec()函数中去了:
我们把实例1中的eval函数换成exec函数试试:
x = 10 def func(): y = 20 a = exec(‘x + y‘) print(‘a: ‘, a) b = exec(‘x + y‘, {‘x‘: 1, ‘y‘: 2}) print(‘b: ‘, b) c = exec(‘x + y‘, {‘x‘: 1, ‘y‘: 2}, {‘y‘: 3, ‘z‘: 4}) print(‘c: ‘, c) d = exec(‘print(x, y)‘) print(‘d: ‘, d) func()
输出结果:
a: None b: None c: None 10 20 d: None
因为我们说过了,exec函数的返回值永远为None。
x = 10 expr = """ z = 30 sum = x + y + z print(sum) """ def func(): y = 20 exec(expr) exec(expr, {‘x‘: 1, ‘y‘: 2}) exec(expr, {‘x‘: 1, ‘y‘: 2}, {‘y‘: 3, ‘z‘: 4}) func()
输出结果:
60 33 34
对输出结果的解释:
前两个输出跟上面解释的eval函数执行过程一样,不做过多解释。关于最后一个数字34,我们可以看出是:x = 1, y = 3是没有疑问的。关于z为什么还是30而不是4,这其实也很简单,我们只需要在理一下代码执行过程就可以了,其执行过程相当于:
x = 1 y = 2 def func(): y = 3 z = 4 z = 30 sum = x + y + z print(sum) func()
先来看下这两个函数的定义和文档描述
globals()
描述: Return a dictionary representing the current global symbol table. This is always the dictionary of the current module (inside a function or method, this is the module where it is defined, not the module from which it is called).
翻译: 返回一个表示当前全局标识符表的字典。这永远是当前模块的字典(在一个函数或方法内部,这是指定义该函数或方法的模块,而不是调用该函数或方法的模块)
locals()
描述: Update and return a dictionary representing the current local symbol table. Free variables are returned by locals() when it is called in function blocks, but not in class blocks.
Note The contents of this dictionary should not be modified; changes may not affect the values of local and free variables used by the interpreter.
翻译: 更新并返回一个表示当前局部标识符表的字典。自由变量在函数内部被调用时,会被locals()函数返回;自由变量在类累不被调用时,不会被locals()函数返回。
注意: locals()返回的字典的内容不应该被改变;如果一定要改变,不应该影响被解释器使用的局部变量和自由变量。
name = ‘Tom‘ age = 18 def func(x, y): sum = x + y _G = globals() _L = locals() print(id(_G), type(_G), _G) print(id(_L), type(_L), _L) func(10, 20)
输出结果:
2131520814344 <class ‘dict‘> {‘__builtins__‘: <module ‘builtins‘ (built-in)>, ‘func‘: <function func at 0x000001F048C5E048>, ‘__doc__‘: None, ‘__file__‘: ‘C:/Users/wader/PycharmProjects/LearnPython/day04/func5.py‘, ‘__loader__‘: <_frozen_importlib_external.SourceFileLoader object at 0x000001F048BF4C50>, ‘__spec__‘: None, ‘age‘: 18, ‘__name__‘: ‘__main__‘, ‘name‘: ‘Tom‘, ‘__package__‘: None, ‘__cached__‘: None} 2131524302408 <class ‘dict‘> {‘y‘: 20, ‘x‘: 10, ‘_G‘: {‘__builtins__‘: <module ‘builtins‘ (built-in)>, ‘func‘: <function func at 0x000001F048C5E048>, ‘__doc__‘: None, ‘__file__‘: ‘C:/Users/wader/PycharmProjects/LearnPython/day04/func5.py‘, ‘__loader__‘: <_frozen_importlib_external.SourceFileLoader object at 0x000001F048BF4C50>, ‘__spec__‘: None, ‘age‘: 18, ‘__name__‘: ‘__main__‘, ‘name‘: ‘Tom‘, ‘__package__‘: None, ‘__cached__‘: None}, ‘sum‘: 30}
name = ‘Tom‘ age = 18 G = globals() L = locals() print(id(G), type(G), G) print(id(L), type(L), L)
输出结果:
2494347312392 <class ‘dict‘> {‘__file__‘: ‘C:/Users/wader/PycharmProjects/LearnPython/day04/func5.py‘, ‘__loader__‘: <_frozen_importlib_external.SourceFileLoader object at 0x00000244C2E44C50>, ‘name‘: ‘Tom‘, ‘__spec__‘: None, ‘__builtins__‘: <module ‘builtins‘ (built-in)>, ‘__cached__‘: None, ‘L‘: {...}, ‘__package__‘: None, ‘__name__‘: ‘__main__‘, ‘G‘: {...}, ‘__doc__‘: None, ‘age‘: 18} 2494347312392 <class ‘dict‘> {‘__file__‘: ‘C:/Users/wader/PycharmProjects/LearnPython/day04/func5.py‘, ‘__loader__‘: <_frozen_importlib_external.SourceFileLoader object at 0x00000244C2E44C50>, ‘name‘: ‘Tom‘, ‘__spec__‘: None, ‘__builtins__‘: <module ‘builtins‘ (built-in)>, ‘__cached__‘: None, ‘L‘: {...}, ‘__package__‘: None, ‘__name__‘: ‘__main__‘, ‘G‘: {...}, ‘__doc__‘: None, ‘age‘: 18}
上面打印出的G和L的内存地址是一样的,说明在模块级别locals()的返回值和globals()的返回值是相同的。
将source编译为code对象或AST对象。code对象能够通过exec()函数来执行或者通过eval()函数进行计算求值。
compile(source, filename, mode[, flags[, dont_inherit]])
s = """ for x in range(10): print(x, end=‘‘) print() """ code_exec = compile(s, ‘<string>‘, ‘exec‘) code_eval = compile(‘10 + 20‘, ‘<string>‘, ‘eval‘) code_single = compile(‘name = input("Input Your Name: ")‘, ‘<string>‘, ‘single‘) a = exec(code_exec) b = eval(code_eval) c = exec(code_single) d = eval(code_single) print(‘a: ‘, a) print(‘b: ‘, b) print(‘c: ‘, c) print(‘name: ‘, name) print(‘d: ‘, d) print(‘name; ‘, name)
输出结果:
0123456789 Input Your Name: Tom Input Your Name: Jerry a: None b: 30 c: None name: Jerry d: None name; Jerry
comiple()函数、globals()函数、locals()函数的返回结果可以当作eval()函数与exec()函数的参数使用。
另外,我们可以通过判断globals()函数的返回值中是否包含某个key来判断,某个全局变量是否已经存在(被定义)。