Skip to content

Commit 73f5124

Browse files
CooCoo
Coo
authored and
Coo
committed
10.10小节完成
1 parent ac13528 commit 73f5124

File tree

1 file changed

+7
-17
lines changed

1 file changed

+7
-17
lines changed

source/c10/p10_import_modules_using_name_given_in_string.rst

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,14 @@
55
----------
66
问题
77
----------
8-
You have the name of a module that you would like to import, but it’s being held in a
9-
string. You would like to invoke the import command on the string.
8+
你想导入一个模块,但是模块的名字在字符串里。你想对字符串调用导入命令。
109

1110
|
1211
1312
----------
1413
解决方案
1514
----------
16-
Use the importlib.import_module() function to manually import a module or part of
17-
a package where the name is given as a string. For example:
15+
使用importlib.import_module()函数来手动导入名字为字符串给出的一个模块或者包的一部分。举个例子:
1816

1917
.. code-block:: python
2018
@@ -26,13 +24,10 @@ a package where the name is given as a string. For example:
2624
>>> u = mod.urlopen('http://www.python.org')
2725
>>>
2826
29-
import_module simply performs the same steps as import, but returns the resulting
30-
module object back to you as a result. You just need to store it in a variable and use it
31-
like a normal module afterward.
27+
import_module只是简单地执行和import相同的步骤,但是返回生成的模块对象。你只需要将其存储在一个变量,然后像正常的模块一样使用。
3228

3329

34-
If you are working with packages, import_module() can also be used to perform relative
35-
imports. However, you need to give it an extra argument. For example:
30+
如果你正在使用的包,import_module()也可用于相对导入。但是,你需要给它一个额外的参数。例如:
3631

3732
.. code-block:: python
3833
@@ -43,15 +38,10 @@ imports. However, you need to give it an extra argument. For example:
4338
----------
4439
讨论
4540
----------
46-
The problem of manually importing modules with import_module() most commonly
47-
arises when writing code that manipulates or wraps around modules in some way. For
48-
example, perhaps you’re implementing a customized importing mechanism of some
49-
kind where you need to load a module by name and perform patches to the loaded code.
41+
使用import_module()手动导入模块的问题通常出现在以某种方式编写修改或覆盖模块的代码时候。例如,也许你正在执行某种自定义导入机制,需要通过名称来加载一个模块,通过补丁加载代码。
5042

5143

52-
In older code, you will sometimes see the built-in __import__() function used to perform
53-
imports. Although this works, importlib.import_module() is usually easier to
54-
use.
44+
在旧的代码,有时你会看到用于导入的内建函数__import__()。尽管它能工作,但是importlib.import_module() 通常更容易使用。
5545

56-
See Recipe 10.11 for an advanced example of customizing the import process.
46+
自定义导入过程的高级实例见10.11小节
5747

0 commit comments

Comments
 (0)