5
5
----------
6
6
问题
7
7
----------
8
- You want to organize your code into a package consisting of a hierarchical collection of
9
- modules.
8
+ 你想将你的代码组织成由很多分层模块构成的包。
10
9
11
10
|
12
11
13
12
----------
14
13
解决方案
15
14
----------
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
+ 例如:
18
17
19
18
.. code-block :: python
20
19
@@ -30,8 +29,7 @@ and make sure that every directory defines an __init__.py file. For example:
30
29
png.py
31
30
jpg.py
32
31
33
- Once you have done this, you should be able to perform various import statements,
34
- such as the following:
32
+ 一旦你做到了这一点,你应该能够执行各种import语句,如下:
35
33
36
34
.. code-block :: python
37
35
@@ -44,37 +42,25 @@ such as the following:
44
42
----------
45
43
讨论
46
44
----------
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导入之前导入。
54
48
55
49
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能够用来自动加载子模块:
59
52
60
53
.. code-block :: python
61
54
62
55
# graphics/formats/__init__.py
63
56
from . import jpg
64
57
from . import png
65
58
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.
68
59
60
+ 像这样一个文件,用户可以仅仅通过import grahpics.formats来代替import graphics.formats.jpg以及import graphics.formats.png。
69
61
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.
73
62
63
+ __init__.py的其他常用用法包括将多个文件合并到一个逻辑命名空间,这将在10.4小节讨论。
74
64
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.
80
65
66
+ 敏锐的程序员会发现,即使没有__init__.py文件存在,python仍然会导入包。如果你没有定义__init__.py时,实际上创建了一个所谓的“命名空间包”,这将在10.5小节讨论。万物平等,如果你着手创建一个新的包的话,包含一个__init__.py文件吧。
0 commit comments