5
5
----------
6
6
问题
7
7
----------
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
+
11
10
12
11
|
13
12
14
13
----------
15
14
解决方案
16
15
----------
17
- A program module can be split into separate files by turning it into a package. Consider
18
- the following simple module:
16
+ 程序模块可以通过变成包来分割成多个独立的文件。考虑下下面简单的模块:
17
+
19
18
20
19
.. code-block :: python
21
20
@@ -28,9 +27,9 @@ the following simple module:
28
27
def bar (self ):
29
28
print (' B.bar' )
30
29
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
+
34
33
35
34
.. code-block :: python
36
35
@@ -39,7 +38,7 @@ that directory, create the following files:
39
38
a.py
40
39
b.py
41
40
42
- In the a.py file, put this code:
41
+ 在a.py文件中插入以下代码:
43
42
44
43
.. code-block :: python
45
44
@@ -48,7 +47,7 @@ In the a.py file, put this code:
48
47
def spam (self ):
49
48
print (' A.spam' )
50
49
51
- In the b.py file, put this code:
50
+ 在b.py文件中插入以下代码:
52
51
53
52
.. code-block :: python
54
53
@@ -58,16 +57,15 @@ In the b.py file, put this code:
58
57
def bar (self ):
59
58
print (' B.bar' )
60
59
61
- Finally, in the __init__.py file, glue the two files together:
60
+ 最后,在 __init__.py 中,将2个文件粘合在一起:
62
61
63
62
.. code-block :: python
64
63
65
64
# __init__.py
66
65
from .a import A
67
66
from .b import B
68
67
69
- If you follow these steps, the resulting mymodule package will appear to be a single logical
70
- module:
68
+ 如果按照这些步骤,所产生的包MyModule将作为一个单一的逻辑模块:
71
69
72
70
.. code-block :: python
73
71
@@ -85,44 +83,34 @@ module:
85
83
----------
86
84
讨论
87
85
----------
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
+
92
88
93
89
.. code-block :: python
94
90
95
91
from mymodule.a import A
96
92
from mymodule.b import B
97
93
...
98
94
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
+
101
97
102
98
.. code-block :: python
103
99
104
100
from mymodule import A, B
105
101
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 文件来将每部分粘合在一起。
110
104
111
105
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 来获取。
115
107
116
108
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小节)
120
110
121
111
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有细微的变化:
126
114
127
115
.. code-block :: python
128
116
@@ -135,8 +123,8 @@ do that, here is a slight variation of __init__.py:
135
123
from .b import B
136
124
return B()
137
125
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
+ 例如:
140
128
141
129
.. code-block :: python
142
130
@@ -146,8 +134,7 @@ classes when they are first accessed. To a user, it won’t look much different.
146
134
A.spam
147
135
>> >
148
136
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
+ 延迟加载的主要缺点是继承和类型检查可能会中断。你可能会稍微改变你的代码,例如:
151
138
152
139
.. code-block :: python
153
140
@@ -157,7 +144,6 @@ For example, you might have to change your code slightly:
157
144
if isinstance (x, mymodule.a.A): # Ok
158
145
...
159
146
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 的源码.
162
148
163
149
0 commit comments