Skip to content

Commit fe7eb35

Browse files
CooCoo
Coo
authored and
Coo
committed
10.6小节完成
1 parent 0692c3f commit fe7eb35

File tree

1 file changed

+11
-22
lines changed

1 file changed

+11
-22
lines changed

source/c10/p06_reloading_modules.rst

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
----------
66
问题
77
----------
8-
You want to reload an already loaded module because you’ve made changes to its source.
8+
你想重新加载已经加载的模块,因为你对其源码进行了修改。
99

1010
|
1111
1212
----------
1313
解决方案
1414
----------
15-
To reload a previously loaded module, use imp.reload(). For example:
15+
使用imp.reload()来重新加载先前加载的模块。举个例子:
1616

1717
.. code-block:: python
1818
@@ -27,19 +27,13 @@ To reload a previously loaded module, use imp.reload(). For example:
2727
----------
2828
讨论
2929
----------
30-
Reloading a module is something that is often useful during debugging and development,
31-
but which is generally never safe in production code due to the fact that it doesn’t
32-
always work as you expect.
30+
重新加载模块在开发和调试过程中常常很有用。但在生产环境中的代码使用会不安全,因为它并不总是像您期望的那样工作。
3331

3432

35-
Under the covers, the reload() operation wipes out the contents of a module’s underlying
36-
dictionary and refreshes it by re-executing the module’s source code. The identity
37-
of the module object itself remains unchanged. Thus, this operation updates the module
38-
everywhere that it has been imported in a program.
33+
reload()擦除了模块底层字典的内容,并通过重新执行模块的源代码来刷新它。模块对象本身的身份保持不变。因此,该操作在程序中所有已经被导入了的地方更新了模块。
3934

4035

41-
However, reload() does not update definitions that have been imported using statements
42-
such as from module import name. To illustrate, consider the following code:
36+
尽管如此,reload()没有更新像"from module import name"这样使用import语句导入的定义。举个例子:
4337

4438
.. code-block:: python
4539
@@ -50,7 +44,7 @@ such as from module import name. To illustrate, consider the following code:
5044
def grok():
5145
print('grok')
5246
53-
Now start an interactive session:
47+
现在启动交互式会话:
5448

5549
.. code-block:: python
5650
@@ -62,15 +56,15 @@ Now start an interactive session:
6256
grok
6357
>>>
6458
65-
Without quitting Python, go edit the source code to spam.py so that the function grok()
66-
looks like this:
59+
不退出Python修改spam.py的源码,将grok()函数改成这样:
60+
6761

6862
.. code-block:: python
6963
7064
def grok():
7165
print('New grok')
7266
73-
Now go back to the interactive session, perform a reload, and try this experiment:
67+
现在回到交互式会话,重新加载模块,尝试下这个实验:
7468

7569
.. code-block:: python
7670
@@ -85,12 +79,7 @@ Now go back to the interactive session, perform a reload, and try this experimen
8579
New grok
8680
>>>
8781
88-
In this example, you’ll observe that there are two versions of the grok() function loaded.
89-
Generally, this is not what you want, and is just the sort of thing that eventually leads
90-
to massive headaches.
91-
82+
在这个例子中,你看到有2个版本的grok()函数被加载。通常来说,这不是你想要的,而是令人头疼的事。
9283

93-
For this reason, reloading of modules is probably something to be avoided in production
94-
code. Save it for debugging or for interactive sessions where you’re experimenting with
95-
the interpreter and trying things out.
9684

85+
因此,在生产环境中可能需要避免重新加载模块。在交互环境下调试,解释程序并试图弄懂它。

0 commit comments

Comments
 (0)