Skip to content

Commit 8fd3d60

Browse files
committed
9,23小节完成
1 parent 819b903 commit 8fd3d60

File tree

1 file changed

+27
-43
lines changed

1 file changed

+27
-43
lines changed

source/c09/p23_executing_code_with_local_side_effects.rst

Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,14 @@
55
----------
66
问题
77
----------
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+
你想在使用范围内执行某个代码片段,并且希望在执行后所有的结果都不可见。
109

1110
|
1211
1312
----------
1413
解决方案
1514
----------
16-
To better understand the problem, try a little experiment. First, execute a fragment of
17-
code in the global namespace:
15+
为了理解这个问题,先试试一个简单场景。首先,在全局命名空间内执行一个代码片段:
1816

1917
.. code-block:: python
2018
@@ -24,7 +22,7 @@ code in the global namespace:
2422
14
2523
>>>
2624
27-
Now, try the same experiment inside a function:
25+
然后,再在一个函数中执行同样的代码:
2826

2927
.. code-block:: python
3028
@@ -40,14 +38,11 @@ Now, try the same experiment inside a function:
4038
NameError: global name 'b' is not defined
4139
>>>
4240
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()`` 执行结果的话就会有问题了。
4643

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+
之后你就能从局部字典中获取修改过后的变量值了。例如:
5146

5247
.. code-block:: python
5348
@@ -67,17 +62,14 @@ modified values from the locals dictionary. For example:
6762
----------
6863
讨论
6964
----------
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+
还有另外更好的解决方案(比如装饰器、闭包、元类等等)。
7467

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+
下面是另外一个演示它的例子:
8173

8274
.. code-block:: python
8375
@@ -90,10 +82,8 @@ that shows this effect:
9082
0
9183
>>>
9284
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+
通过在代码执行后审查这个字典的值,那就能获取修改后的值了。下面是一个演示例子:
9787

9888
.. code-block:: python
9989
@@ -111,13 +101,11 @@ this:
111101
x = 0
112102
>>>
113103
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变量值是不会变的。
117105

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+
请注意观察下下面这个试验的输出结果:
121109

122110
.. code-block:: python
123111
@@ -136,10 +124,9 @@ the corresponding entries in the dictionary. Observe the outcome of this experim
136124
{'loc': {...}, 'x': 0}
137125
>>>
138126
139-
Notice how the last call to locals() caused x to be overwritten.
127+
注意最后一次调用 ``locals()`` 的时候x的值是如何被覆盖掉的。
140128

141-
As an alternative to using locals(), you might make your own dictionary and pass it
142-
to exec(). For example:
129+
作为 ``locals()`` 的一个替代方案,你可以使用你自己的字典,并将它传递给 ``exec()`` 。例如:
143130

144131
.. code-block:: python
145132
@@ -155,12 +142,9 @@ to exec(). For example:
155142
14
156143
>>>
157144
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+
你只需要保证全局和局部字典在后面代码访问时已经被初始化。
166147

148+
还有一点,在使用 ``exec()`` 之前,你可能需要问下自己是否有其他更好的替代方案。
149+
大多数情况下当你要考虑使用 ``exec()`` 的时候,
150+
还有另外更好的解决方案,比如装饰器、闭包、元类,或其他一些元编程特性。

0 commit comments

Comments
 (0)