转发:http://www.cnblogs.com/yyds/p/6276746.htmlhtml
计算指定表达式的值。也就是说它要执行的Python代码只能是单个运算表达式(注意eval不支持任意形式的赋值操做),而不能是复杂的代码逻辑,这一点和lambda表达式比较类似。python
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()
输出结果:express
a: 30 b: 3 c: 4 10 20 d: None
对输出结果的解释:ide
动态执行Python代码。也就是说exec能够执行复杂的Python代码,而不像eval函数那么样只能计算一个表达式的值。函数
exec(object[, globals[, locals]])
exec函数的返回值永远为None.ui
须要说明的是在Python 2中exec不是函数,而是一个内置语句(statement),可是Python 2中有一个execfile()函数。能够理解为Python 3把exec这个statement和execfile()函数的功可以整合到一个新的exec()函数中去了:this
# !usr/bin/env python # coding:utf-8 _globals = {'name': 'tom', 'data': [18, 73, 84]} _locals = {'a': 1, 'b': 2} code = ''' def hellocute(): return "name %s ,age %d" %(name,data[0],) ''' func = compile(code, '<string>', "exec") """ 一、func里输入的变量(上面的name和data),先从_globals传入,若是_globals=None,则从globals()传入 二、func输出的函数,先输出到_locals,若是_locals=None,则输出到_globals,若是_globals=None,则输出到globals() 状况1 exec(func) # 默认参数均为None name = "tom" # 因为上面传入None,因此须要在全局定义 data = [1,2,3,4] print(hellocute()) # name tom ,age 1 func从全局即globals传入参数,同时新建立的变量如func也输出到globals里 状况2 exec(func,_globals,None) print(_globals["hellocute"]()) # name tom ,age 18 print(_globals.keys()) # dict_keys(['name', 'data', '__builtins__', 'hellocute']) 若是存在字典参数_globals,而_locals为None,则输入输出都到_globals里,同时,_globals会也会被传入一个k是__builtins__,v是globals()+python自带函数等 状况3 exec(func,None,_locals) # print(_locals["hellocute"]()) # 报错 print(_locals.keys()) # dict_keys(['a', 'b', 'hellocute']) # name/data从globals(),因为未定义,因此若是func执行的话会报错 # func名传入_locals 状况4 exec(func,_globals,_locals) print(_globals.keys()) # dict_keys(['name', 'data', '__builtins__']) print(_locals.keys()) # dict_keys(['a', 'b', 'hellocute']) print(_locals["hellocute"]()) # name tom ,age 18 从_globals传入name/data,"hellocute"输出到_locals """
咱们把实例1中的eval函数换成exec函数试试:spa
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。code
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来判断,某个全局变量是否已经存在(被定义)。