Skip to content

Commit d2efadd

Browse files
committed
Merge pull request yidao620c#40 from MoguCloud/master
10.4小节完成
2 parents 1a449cd + ac8a4d4 commit d2efadd

File tree

1 file changed

+25
-39
lines changed

1 file changed

+25
-39
lines changed

source/c10/p04_split_module_into_multiple_files.rst

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,16 @@
55
----------
66
问题
77
----------
8-
You have a module that you would like to split into multiple files. However, you would
9-
like to do it without breaking existing code by keeping the separate files unified as a
10-
single logical module.
8+
你想将一个模块分割成多个文件。但是你不想将分离的文件统一成一个逻辑模块时使已有的代码遭到破坏。
9+
1110

1211
|
1312
1413
----------
1514
解决方案
1615
----------
17-
A program module can be split into separate files by turning it into a package. Consider
18-
the following simple module:
16+
程序模块可以通过变成包来分割成多个独立的文件。考虑下下面简单的模块:
17+
1918

2019
.. code-block:: python
2120
@@ -28,9 +27,9 @@ the following simple module:
2827
def bar(self):
2928
print('B.bar')
3029
31-
Suppose you want to split mymodule.py into two files, one for each class definition. To
32-
do that, start by replacing the mymodule.py file with a directory called mymodule. In
33-
that directory, create the following files:
30+
假设你想mymodule.py分为两个文件,每个定义的一个类。要做到这一点,首先用mymodule目录来替换文件mymodule.py。
31+
这这个目录下,创建以下文件:
32+
3433

3534
.. code-block:: python
3635
@@ -39,7 +38,7 @@ that directory, create the following files:
3938
a.py
4039
b.py
4140
42-
In the a.py file, put this code:
41+
在a.py文件中插入以下代码:
4342

4443
.. code-block:: python
4544
@@ -48,7 +47,7 @@ In the a.py file, put this code:
4847
def spam(self):
4948
print('A.spam')
5049
51-
In the b.py file, put this code:
50+
在b.py文件中插入以下代码:
5251

5352
.. code-block:: python
5453
@@ -58,16 +57,15 @@ In the b.py file, put this code:
5857
def bar(self):
5958
print('B.bar')
6059
61-
Finally, in the __init__.py file, glue the two files together:
60+
最后,在 __init__.py 中,将2个文件粘合在一起:
6261

6362
.. code-block:: python
6463
6564
# __init__.py
6665
from .a import A
6766
from .b import B
6867
69-
If you follow these steps, the resulting mymodule package will appear to be a single logical
70-
module:
68+
如果按照这些步骤,所产生的包MyModule将作为一个单一的逻辑模块:
7169

7270
.. code-block:: python
7371
@@ -85,44 +83,34 @@ module:
8583
----------
8684
讨论
8785
----------
88-
The primary concern in this recipe is a design question of whether or not you want
89-
users to work with a lot of small modules or just a single module. For example, in a large
90-
code base, you could just break everything up into separate files and make users use a
91-
lot of import statements like this:
86+
在这个章节中的主要问题是一个设计问题,不管你是否希望用户使用很多小模块或只是一个模块。举个例子,在一个大型的代码库中,你可以将这一切都分割成独立的文件,让用户使用大量的import语句,就像这样:
87+
9288

9389
.. code-block:: python
9490
9591
from mymodule.a import A
9692
from mymodule.b import B
9793
...
9894
99-
This works, but it places more of a burden on the user to know where the different parts
100-
are located. Often, it’s just easier to unify things and allow a single import like this:
95+
这样能工作,但这让用户承受更多的负担,用户要知道不同的部分位于何处。通常情况下,将这些统一起来,使用一条import将更加容易,就像这样:
96+
10197

10298
.. code-block:: python
10399
104100
from mymodule import A, B
105101
106-
For this latter case, it’s most common to think of mymodule as being one large source
107-
file. However, this recipe shows how to stitch multiple files together into a single logical
108-
namespace. The key to doing this is to create a package directory and to use the
109-
__init__.py file to glue the parts together.
102+
对后者而言,让mymodule成为一个大的源文件是最常见的。但是,这一章节展示了如何合并多个文件合并成一个单一的逻辑命名空间。
103+
这样做的关键是创建一个包目录,使用 __init__.py 文件来将每部分粘合在一起。
110104

111105

112-
When a module gets split, you’ll need to pay careful attention to cross-filename references.
113-
For instance, in this recipe, class B needs to access class A as a base class. A packagerelative
114-
import from .a import A is used to get it.
106+
当一个模块被分割,你需要特别注意交叉引用的文件名。举个例子,在这一章节中,B类需要访问A类作为基类。用包的相对导入 from .a import A 来获取。
115107

116108

117-
Package-relative imports are used throughout the recipe to avoid hardcoding the toplevel
118-
module name into the source code. This makes it easier to rename the module or
119-
move it around elsewhere later (see Recipe 10.3).
109+
整个章节都使用包的相对导入来避免将顶层模块名硬编码到源代码中。这使得重命名模块或者将它移动到别的位置更容易。(见10.3小节)
120110

121111

122-
One extension of this recipe involves the introduction of “lazy” imports. As shown, the
123-
__init__.py file imports all of the required subcomponents all at once. However, for a
124-
very large module, perhaps you only want to load components as they are needed. To
125-
do that, here is a slight variation of __init__.py:
112+
作为这一章节的延伸,将介绍延迟导入。如图所示,__init__.py文件一次导入所有必需的组件的。但是对于一个很大的模块,可能你只想组件在需要时被加载。
113+
要做到这一点,__init__.py有细微的变化:
126114

127115
.. code-block:: python
128116
@@ -135,8 +123,8 @@ do that, here is a slight variation of __init__.py:
135123
from .b import B
136124
return B()
137125
138-
In this version, classes A and B have been replaced by functions that load the desired
139-
classes when they are first accessed. To a user, it won’t look much different. For example:
126+
在这个版本中,类A和类B被替换为在第一次访问时加载所需的类的函数。对于用户,这看起来不会有太大的不同。
127+
例如:
140128

141129
.. code-block:: python
142130
@@ -146,8 +134,7 @@ classes when they are first accessed. To a user, it won’t look much different.
146134
A.spam
147135
>>>
148136
149-
The main downside of lazy loading is that inheritance and type checking might break.
150-
For example, you might have to change your code slightly:
137+
延迟加载的主要缺点是继承和类型检查可能会中断。你可能会稍微改变你的代码,例如:
151138

152139
.. code-block:: python
153140
@@ -157,7 +144,6 @@ For example, you might have to change your code slightly:
157144
if isinstance(x, mymodule.a.A): # Ok
158145
...
159146
160-
For a real-world example of lazy loading, look at the source code for multiprocessing/
161-
__init__.py in the standard library.
147+
延迟加载的真实例子, 见标准库 multiprocessing/__init__.py 的源码.
162148

163149

0 commit comments

Comments
 (0)