我无法理解Python中“eval()”和“exec”的语义。 (此问题中的所有代码在Python 2.7.8和Python 3.4.2中的行为方式相同)。 “eval”的documentation表示:
如果省略[locals和globals],则执行表达式 在调用eval()的环境中。
“exec”有类似的语言。我显然不理解这句话,因为我希望以下程序定义的四个函数可以做同样的事情。
def h(x):
ls = locals()
exec('def i(y): return (w, x, y)', globals(), ls)
i = ls['i']
def j(y): return (w, x, y)
k = eval('lambda y: (w, x, y)')
l = lambda y: (w, x, y)
return i, j, k, l
w = 1
i, j, k, l = h(2)
他们没有。
>>> i(3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in i
NameError: name 'x' is not defined
>>> j(3)
(1, 2, 3)
>>> k(3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <lambda>
NameError: name 'x' is not defined
>>> l(3)
(1, 2, 3)
反汇编代码揭示了原因:“x”被“eval”和“exec”视为全局变量。
from dis import dis
print("This is `i`:")
dis(i)
print("This is `j`:")
dis(j)
print("This is `k`:")
dis(k)
print("This is `l`:")
dis(l)
print("For reference, this is `h`:")
dis(h)
输出:
This is `i`:
1 0 LOAD_GLOBAL 0 (w)
3 LOAD_GLOBAL 1 (x)
6 LOAD_FAST 0 (y)
9 BUILD_TUPLE 3
12 RETURN_VALUE
This is `j`:
25 0 LOAD_GLOBAL 0 (w)
3 LOAD_DEREF 0 (x)
6 LOAD_FAST 0 (y)
9 BUILD_TUPLE 3
12 RETURN_VALUE
This is `k`:
1 0 LOAD_GLOBAL 0 (w)
3 LOAD_GLOBAL 1 (x)
6 LOAD_FAST 0 (y)
9 BUILD_TUPLE 3
12 RETURN_VALUE
This is `l`:
27 0 LOAD_GLOBAL 0 (w)
3 LOAD_DEREF 0 (x)
6 LOAD_FAST 0 (y)
9 BUILD_TUPLE 3
12 RETURN_VALUE
For reference, this is `h`:
22 0 LOAD_NAME 0 (locals)
3 CALL_FUNCTION 0
6 STORE_FAST 1 (ls)
23 9 LOAD_CONST 1 ('def i(y): return (w, x, y)')
12 LOAD_NAME 1 (globals)
15 CALL_FUNCTION 0
18 LOAD_FAST 1 (ls)
21 EXEC_STMT
24 22 LOAD_FAST 1 (ls)
25 LOAD_CONST 2 ('i')
28 BINARY_SUBSCR
29 STORE_FAST 2 (i)
25 32 LOAD_CLOSURE 0 (x)
35 BUILD_TUPLE 1
38 LOAD_CONST 3 (<code object j at 0x7ffc3843c030, file "test.py", line 25>)
41 MAKE_CLOSURE 0
44 STORE_FAST 3 (j)
26 47 LOAD_NAME 2 (eval)
50 LOAD_CONST 4 ('lambda y: (w, x, y)')
53 CALL_FUNCTION 1
56 STORE_FAST 4 (k)
27 59 LOAD_CLOSURE 0 (x)
62 BUILD_TUPLE 1
65 LOAD_CONST 5 (<code object <lambda> at 0x7ffc3843c3b0, file "test.py", line 27>)
68 MAKE_CLOSURE 0
71 STORE_FAST 5 (l)
28 74 LOAD_FAST 2 (i)
77 LOAD_FAST 3 (j)
80 LOAD_FAST 4 (k)
83 LOAD_FAST 5 (l)
86 BUILD_TUPLE 4
89 RETURN_VALUE
上面的“j”和“l”有我想要的行为。如何使用“eval”或“exec”获得此行为?
使用类而不是函数作为外部包装器会改变语义,但与所需方式相反。它使“x”成为一个全球性的。
class H:
x = 2
f = staticmethod(eval('lambda y: (w, x, y)'))
H.dis(H.f)
w = 1
H.f(3)
输出:
1 0 LOAD_GLOBAL 0 (w)
3 LOAD_GLOBAL 1 (x)
6 LOAD_FAST 0 (y)
9 BUILD_TUPLE 3
12 RETURN_VALUE
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <lambda>
NameError: global name 'x' is not defined
包装“classmethod”或将其保留为未绑定的实例方法只会让事情变得更糟。
使用字符串插值替换“x”适用于整数:
def h(x):
return eval('lambda y: (w, %r, y)' % x)
k = h(2)
dis(k)
w = 1
k(3)
输出:
1 0 LOAD_GLOBAL 0 (w)
3 LOAD_CONST 1 (2)
6 LOAD_FAST 0 (y)
9 BUILD_TUPLE 3
12 RETURN_VALUE
(1, 2, 3)
但是,我不想假设“x”可以无损地转换为字符串并返回。以下示例中打破了此尝试:
k = h(lambda: "something")
k = h(open('some_file', 'w'))
cell = ["Wrong value"]
k = h(cell)
cell[0] = "Right value"
k(3)
由于Python正在寻找一个全局变量,一个明显的尝试是将“x”作为全局变量传递:
def h(x):
my_globals = {'w': w, 'x': x}
return eval('lambda y: (w, x, y)', my_globals)
k = h(2)
dis(k)
w = 1
k(3)
输出:
1 0 LOAD_GLOBAL 0 (w)
3 LOAD_GLOBAL 1 (x)
6 LOAD_FAST 0 (y)
9 BUILD_TUPLE 3
12 RETURN_VALUE
(1, 2, 3)
这种尝试被破坏了,因为它太早地读取了“w”的值:
w = "Wrong value"
k = h(2)
w = "Right value"
k(3)
我最终找到了一种有效的方法,但我真的不喜欢它:
def h(x):
return eval('lambda x: lambda y: (w, x, y)')(x)
k = h(2)
dis(k)
w = 1
k(3)
输出:
1 0 LOAD_GLOBAL 0 (w)
3 LOAD_DEREF 0 (x)
6 LOAD_FAST 0 (y)
9 BUILD_TUPLE 3
12 RETURN_VALUE
(1, 2, 3)
特别是,如果我不知道我传递给“eval”的字符串捕获的局部变量的完整列表,这将会变得很痛苦。
你能做得更好吗?
更新2014-12-25
寻找更多创建局部变量“x”的方法,我尝试了这个:
def h(x):
ls = locals()
exec('x = x\ndef i(y): return (w, x, y)', globals(), ls)
exec('_ = x\ndef j(y): return (w, x, y)', globals(), ls)
return ls['i'], ls['j'], ls['_'], ls['x']
i, j, check1, check2 = h(2)
assert check1 == 2
assert check2 == 2
w = 1
print("This is `i`:")
dis(i)
print("This is `j`:")
dis(j)
print("i(3) = %r" % (i(3),))
print("j(3) = %r" % (j(3),))
对“x”的额外分配无效。断言验证“x”在本地词典中,但不是由lambda捕获的。这是输出:
This is `i`:
2 0 LOAD_GLOBAL 0 (w)
3 LOAD_GLOBAL 1 (x)
6 LOAD_FAST 0 (y)
9 BUILD_TUPLE 3
12 RETURN_VALUE
This is `j`:
2 0 LOAD_GLOBAL 0 (w)
3 LOAD_GLOBAL 1 (x)
6 LOAD_FAST 0 (y)
9 BUILD_TUPLE 3
12 RETURN_VALUE
对“i”和“j”的调用都崩溃,抱怨没有全局变量“x”。
[编辑2014-12-29:仅在Python 3上成功。]
创建局部变量的另一种方法是这样的:
def h(x):
i = eval('[lambda y: (w, x, y) for x in [x]][0]')
j = eval('[lambda y: (w, x, y) for _ in [x]][0]')
return i, j
i, j = h(2)
w = 1
print("This is `i`:")
dis(i)
print("This is `j`:")
dis(j)
print("i(3) = %r" % (i(3),))
print("j(3) = %r" % (j(3),))
奇怪的是,在这种情况下,对“x”的额外赋值确实有效。这确实有效,即“i”与“j”不同。这是输出:
This is `i`:
1 0 LOAD_GLOBAL 0 (w)
3 LOAD_DEREF 0 (x)
6 LOAD_FAST 0 (y)
9 BUILD_TUPLE 3
12 RETURN_VALUE
This is `j`:
1 0 LOAD_GLOBAL 0 (w)
3 LOAD_GLOBAL 1 (x)
6 LOAD_FAST 0 (y)
9 BUILD_TUPLE 3
12 RETURN_VALUE
i(3) = (1, 2, 3)
对“j”的调用崩溃,抱怨没有全局“x”,但“i”按需运行并且具有正确的字节码。
为什么这样做,而上面的“失败4”却没有?确定是否可以捕获本地“x”的规则是什么?这个设计的历史是什么? (这看起来很荒谬!)
答案 0 :(得分:1)
我认为您希望您创建的函数继承创建它们的函数的本地环境,但也真实的全局环境(创建它们的函数)。这就是为什么你不喜欢他们把x称为全球,对吧?
以下内容创建了一个&#34;包装器&#34;函数围绕所需函数,所有函数都在同一个exec字符串中。当您调用或重新调用包装器时,创建函数的本地值将在中传递,从而创建一个新的包装闭包。
代码对在locals上下文中创建的新变量很敏感。确保函数和包装器名称都已知并且在那里具有值时会遇到一些麻烦。
def wrap_f(code, gs, ls, wrapper_name, function_name):
ls[function_name] = ls[wrapper_name] = None
arglist = ",".join(ls.keys())
wrapcode = """
def {wrapper_name}({arglist}):
{code}
return {function_name}
""".format(wrapper_name=wrapper_name, arglist=arglist,
code=code, function_name=function_name)
exec(wrapcode, gs, ls)
wrapper = ls[wrapper_name]
return wrapper, wrapper(**ls)
所以,回答原始问题,这段代码......
def h(x):
mcode = " def m(y): return (w, x, y)" # must be indented 4 spaces.
mwrap, m = wrap_f(mcode, globals(), locals(), "mwrap", "m")
return m
w = 1
m = h(2)
print m(3)
...产生这个输出:
(1, 2, 3)
此示例显示了创建者函数中的局部变化时要执行的操作:
def foo(x):
barleycode = """
def barley(y):
print "barley's x =", x
print "barley's y =", y
"""
barleywrap, barley = wrap_f(barleycode, globals(), locals(),
"barleywrap", "barley")
barley("this string")
print
x = "modified x"
barley = barleywrap(**locals())
barley("new arg")
print
x = "re-modified x"
barley("doesn't see the re-mod.")
x = "the global x"
foo("outer arg")
这会产生输出:
barley's x = outer arg
barley's y = this string
barley's x = modified x
barley's y = new arg
barley's x = modified x
barley's y = doesn't see the re-mod.
答案 1 :(得分:0)
我不确定自己是否已经得到它,但我会尽我所能: 我想当你运行eval / exec python不明白它是在函数内部时,我真的不知道为什么。 我尝试做的是使用像这样的格式字符串
k = eval("lambda y: (w, {0}, y)".format(x))
我不确定这件事是否有效。 另外,为什么需要以这种方式使用eval和exec?
答案 2 :(得分:0)
我将分享我自己的理解,为什么python会像这样
每次使用不在参数列表中的变量时,python都会为其创建一个闭包。例如
y
def h(x):
l =lambda y: (w, x,y)
创建一个捕获x的闭包,您可以通过访问
进行检查l.__closure__
这将向您显示x与函数创建一起存储。 但是,由于y被定义为全局变量,因此不与该函数一起存储
运行A.f()
class A:
c = 1
f = lambda :c+1
因为python将在未定义c
的全局命名空间中查找c
Python 3's doc to exec function说
如果exec获得两个单独的对象作为 globals 和 locals ,则代码 将被执行,就像嵌入在 class 定义中一样。
这说明了为什么lambda不能在本地名称空间中捕获变量
k = eval('lambda y: (w, x, y)',dict(globals(),**))