when someone abandons you,it is him that gets loss because he lost someone who truly loves him but you just lost one who doesn’t love you.
函数的作用:
计算指定表达式的值。也就是说它要执行的 Python 代码只能是单个运算表达式(注意 eval 不支持任意形式的赋值操作),而不能是复杂的代码逻辑,这一点和 lambda 表达式比较相似。
函数定义:
eval(expression, globals=None, locals=None)
参数说明:
返回值:
实例
1 | x = 10 |
函数的作用:
动态执行Python代码。也就是说exec可以执行复杂的Python代码,而不像eval函数那么样只能计算一个表达式的值。
函数定义:
exec(object[, globals[, locals]])
参数说明:
返回值:
注意:
需要说明的是在 Python2 中 exec 不是函数,而是一个内置语句(statement),但是 Python2 中有一个 execfile() 函数。可以理解为 Python3 把 exec 这个 statement 和 execfile() 函数的功能够整合到一个新的 exec() 函数中去了:
eval() 函数只能计算单个表达式的值,而 exec() 函数可以动态运行代码段。
eval() 函数可以有返回值,而 exec() 函数返回值永远为 None。
实例1
1 | x = 10 |
实例2
1 | x = 10 |
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() 返回的字典的内容不应该被改变;如果一定要改变,不应该影响被解释器使用的局部变量和自由变量。
总结:
实例1
1 | name = 'Tom' |
实例2
1 | name = 'Tom' |
函数的作用:
将 source 编译为 code 对象或 AST 对象。code 对象能够通过 exec() 函数来执行或者通过 eval() 函数进行计算求值。
函数定义:
compile(source, filename, mode[, flags[, dont_inherit]])
参数说明:
实例:
1 | s = """ |
be slow to promise and quick to perform.