5
5
----------
6
6
问题
7
7
----------
8
- You are using exec() to execute a fragment of code in the scope of the caller, but after
9
- execution, none of its results seem to be visible.
8
+ 你想在使用范围内执行某个代码片段,并且希望在执行后所有的结果都不可见。
10
9
11
10
|
12
11
13
12
----------
14
13
解决方案
15
14
----------
16
- To better understand the problem, try a little experiment. First, execute a fragment of
17
- code in the global namespace:
15
+ 为了理解这个问题,先试试一个简单场景。首先,在全局命名空间内执行一个代码片段:
18
16
19
17
.. code-block :: python
20
18
@@ -24,7 +22,7 @@ code in the global namespace:
24
22
14
25
23
>> >
26
24
27
- Now, try the same experiment inside a function:
25
+ 然后,再在一个函数中执行同样的代码:
28
26
29
27
.. code-block :: python
30
28
@@ -40,14 +38,11 @@ Now, try the same experiment inside a function:
40
38
NameError : global name ' b' is not defined
41
39
>> >
42
40
43
- As you can see, it fails with a NameError almost as if the exec() statement never actually
44
- executed. This can be a problem if you ever want to use the result of the exec() in a
45
- later calculation.
41
+ 可以看出,最后抛出了一个NameError异常,就跟在 ``exec() `` 语句从没执行过一样。
42
+ 要是你想在后面的计算中使用到 ``exec() `` 执行结果的话就会有问题了。
46
43
47
-
48
- To fix this kind of problem, you need to use the locals() function to obtain a dictionary
49
- of the local variables prior to the call to exec(). Immediately afterward, you can extract
50
- modified values from the locals dictionary. For example:
44
+ 为了修正这样的错误,你需要在调用 ``exec() `` 之前使用 ``locals() `` 函数来得到一个局部变量字典。
45
+ 之后你就能从局部字典中获取修改过后的变量值了。例如:
51
46
52
47
.. code-block :: python
53
48
@@ -67,17 +62,14 @@ modified values from the locals dictionary. For example:
67
62
----------
68
63
讨论
69
64
----------
70
- Correct use of exec() is actually quite tricky in practice. In fact, in most situations where
71
- you might be considering the use of exec(), a more elegant solution probably exists
72
- (e.g., decorators, closures, metaclasses, etc.).
73
-
65
+ 实际上对于 ``exec() `` 的正确使用是比较难的。大多数情况下当你要考虑使用 ``exec() `` 的时候,
66
+ 还有另外更好的解决方案(比如装饰器、闭包、元类等等)。
74
67
75
- However, if you still must use exec(), this recipe outlines some subtle aspects of using
76
- it correctly. By default, exec() executes code in the local and global scope of the caller.
77
- However, inside functions, the local scope passed to exec() is a dictionary that is a copy
78
- of the actual local variables. Thus, if the code in exec() makes any kind of modification,
79
- that modification is never reflected in the actual local variables. Here is another example
80
- that shows this effect:
68
+ 然而,如果你仍然要使用 ``exec() `` ,本节列出了一些如何正确使用它的方法。
69
+ 默认情况下,``exec() `` 会在调用者局部和全局范围内执行代码。然而,在函数里面,
70
+ 传递给 ``exec() `` 的局部范围是拷贝实际局部变量组成的一个字典。
71
+ 因此,如果 ``exec() `` 如果执行了修改操作,这种修改后的结果对实际局部变量值是没有影响的。
72
+ 下面是另外一个演示它的例子:
81
73
82
74
.. code-block :: python
83
75
@@ -90,10 +82,8 @@ that shows this effect:
90
82
0
91
83
>> >
92
84
93
- When you call locals() to obtain the local variables, as shown in the solution, you get
94
- the copy of the locals that is passed to exec(). By inspecting the value of the dictionary
95
- after execution, you can obtain the modified values. Here is an experiment that shows
96
- this:
85
+ 上面代码里,当你调用 ``locals() `` 获取局部变量时,你获得的是传递给 ``exec() `` 的局部变量的一个拷贝。
86
+ 通过在代码执行后审查这个字典的值,那就能获取修改后的值了。下面是一个演示例子:
97
87
98
88
.. code-block :: python
99
89
@@ -111,13 +101,11 @@ this:
111
101
x = 0
112
102
>> >
113
103
114
- Carefully observe the output of the last step. Unless you copy the modified value from
115
- loc back to x, the variable remains unchanged.
116
-
104
+ 仔细观察最后一步的输出,除非你将 ``loc `` 中被修改后的值手动赋值给x,否则x变量值是不会变的。
117
105
118
- With any use of locals(), you need to be careful about the order of operations. Each
119
- time it is invoked, locals() will take the current value of local variables and overwrite
120
- the corresponding entries in the dictionary. Observe the outcome of this experiment:
106
+ 在使用 `` locals() `` 的时候,你需要注意操作顺序。每次它被调用的时候,
107
+ `` locals() `` 会获取局部变量值中的值并覆盖字典中相应的变量。
108
+ 请注意观察下下面这个试验的输出结果:
121
109
122
110
.. code-block :: python
123
111
@@ -136,10 +124,9 @@ the corresponding entries in the dictionary. Observe the outcome of this experim
136
124
{' loc' : {... }, ' x' : 0 }
137
125
>> >
138
126
139
- Notice how the last call to locals() caused x to be overwritten.
127
+ 注意最后一次调用 `` locals() `` 的时候x的值是如何被覆盖掉的。
140
128
141
- As an alternative to using locals(), you might make your own dictionary and pass it
142
- to exec(). For example:
129
+ 作为 ``locals() `` 的一个替代方案,你可以使用你自己的字典,并将它传递给 ``exec() `` 。例如:
143
130
144
131
.. code-block :: python
145
132
@@ -155,12 +142,9 @@ to exec(). For example:
155
142
14
156
143
>> >
157
144
158
- For most uses of exec(), this is probably good practice. You just need to make sure that
159
- the global and local dictionaries are properly initialized with names that the executed
160
- code will access.
161
-
162
-
163
- Last, but not least, before using exec(), you might ask yourself if other alternatives are
164
- available. Many problems where you might consider the use of exec() can be replaced
165
- by closures, decorators, metaclasses, or other metaprogramming features.
145
+ 大部分情况下,这种方式是使用 ``exec() `` 的最佳实践。
146
+ 你只需要保证全局和局部字典在后面代码访问时已经被初始化。
166
147
148
+ 还有一点,在使用 ``exec() `` 之前,你可能需要问下自己是否有其他更好的替代方案。
149
+ 大多数情况下当你要考虑使用 ``exec() `` 的时候,
150
+ 还有另外更好的解决方案,比如装饰器、闭包、元类,或其他一些元编程特性。
0 commit comments