Skip to content

Commit baf3e9a

Browse files
committed
Merge pull request yidao620c#33 from MoguCloud/master
10.1小节翻译
2 parents 00361c0 + 72eb7b0 commit baf3e9a

File tree

1 file changed

+12
-26
lines changed

1 file changed

+12
-26
lines changed

source/c10/p01_make_hierarchical_package_of_modules.rst

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,15 @@
55
----------
66
问题
77
----------
8-
You want to organize your code into a package consisting of a hierarchical collection of
9-
modules.
8+
你想将你的代码组织成由很多分层模块构成的包。
109

1110
|
1211
1312
----------
1413
解决方案
1514
----------
16-
Making a package structure is simple. Just organize your code as you wish on the filesystem
17-
and make sure that every directory defines an __init__.py file. For example:
15+
封装成包是很简单的。在文件系统上组织你的代码,并确保每个目录都定义了一个__init__.py文件。
16+
例如:
1817

1918
.. code-block:: python
2019
@@ -30,8 +29,7 @@ and make sure that every directory defines an __init__.py file. For example:
3029
png.py
3130
jpg.py
3231
33-
Once you have done this, you should be able to perform various import statements,
34-
such as the following:
32+
一旦你做到了这一点,你应该能够执行各种import语句,如下:
3533

3634
.. code-block:: python
3735
@@ -44,37 +42,25 @@ such as the following:
4442
----------
4543
讨论
4644
----------
47-
Defining a hierarchy of modules is as easy as making a directory structure on the filesystem.
48-
The purpose of the __init__.py files is to include optional initialization code
49-
that runs as different levels of a package are encountered. For example, if you have the
50-
statement import graphics, the file graphics/__init__.py will be imported and form
51-
the contents of the graphics namespace. For an import such as import graphics.for
52-
mats.jpg, the files graphics/__init__.py and graphics/formats/__init__.py will both be
53-
imported prior to the final import of the graphics/formats/jpg.py file.
45+
定义模块的层次结构就像在文件系统上建立目录结构一样容易。
46+
文件__init__.py的目的是要包含不同运行级别的包的可选的初始化代码。
47+
举个例子,如果你执行了语句import graphics, 文件graphics/__init__.py将被导入,建立graphics命名空间的内容。像import graphics.format.jpg这样导入,文件graphics/__init__.py和文件graphics/graphics/formats/__init__.py将在文件graphics/formats/jpg.py导入之前导入。
5448

5549

56-
More often that not, it’s fine to just leave the __init__.py files empty. However, there are
57-
certain situations where they might include code. For example, an __init__.py file can
58-
be used to automatically load submodules like this:
50+
绝大部分时候让__init__.py空着就好。但是有些情况下可能包含代码。
51+
举个例子,__init__.py能够用来自动加载子模块:
5952

6053
.. code-block:: python
6154
6255
# graphics/formats/__init__.py
6356
from . import jpg
6457
from . import png
6558
66-
For such a file, a user merely has to use a single import graphics.formats instead of
67-
a separate import for graphics.formats.jpg and graphics.formats.png.
6859
60+
像这样一个文件,用户可以仅仅通过import grahpics.formats来代替import graphics.formats.jpg以及import graphics.formats.png。
6961

70-
Other common uses of __init__.py include consolidating definitions from multiple files
71-
into a single logical namespace, as is sometimes done when splitting modules. This is
72-
discussed in Recipe 10.4.
7362

63+
__init__.py的其他常用用法包括将多个文件合并到一个逻辑命名空间,这将在10.4小节讨论。
7464

75-
Astute programmers will notice that Python 3.3 still seems to perform package imports
76-
even if no __init__.py files are present. If you don’t define __init__.py, you actually
77-
create what’s known as a “namespace package,” which is described in Recipe 10.5. All
78-
things being equal, include the __init__.py files if you’re just starting out with the creation
79-
of a new package.
8065

66+
敏锐的程序员会发现,即使没有__init__.py文件存在,python仍然会导入包。如果你没有定义__init__.py时,实际上创建了一个所谓的“命名空间包”,这将在10.5小节讨论。万物平等,如果你着手创建一个新的包的话,包含一个__init__.py文件吧。

0 commit comments

Comments
 (0)