学校的Lab作业需要使用Package功能,顺便翻译一下Package的文档
5.2. Packages
Python has only one type of module object, and all modules are of this type, regardless of whether the module is implemented in Python, C, or something else. To help organize modules and provide a naming hierarchy, Python has a concept of packages.
You can think of packages as the directories on a file system and modules as files within directories, but don’t take this analogy too literally since packages and modules need not originate from the file system. For the purposes of this documentation, we’ll use this convenient analogy of directories and files. Like file system directories, packages are organized hierarchically, and packages may themselves contain subpackages, as well as regular modules.
It’s important to keep in mind that all packages are modules, but not all modules are packages. Or put another way, packages are just a special kind of module. Specifically, any module that contains a
__path__
attribute is considered a package.All modules have a name. Subpackage names are separated from their parent package name by dots, akin to Python’s standard attribute access syntax. Thus you might have a module called
sys
and a package calledemail.mime
and a module within that subpackage calledemail.mime.text.
5.2. 包(Package)
Python只有一种模块(Module)类型,不论这个模块被再何处,何种语言执行。为了更好的管理这些模块,并提供一种命名结构,Python引入了包的概念。
你可以认为包是文件系统中的文件夹,而模块则是文件夹中的文件。但别对这个比喻过度的解读,因为包与模块并不由文件系统确立。为了这篇文档更佳容易理解,我们使用了这个方便形象的类比。与文件系统相同,包是被分级存放组织的,而且包内部也可以存在子包,这点与模块类似。
十分重要的一点是,所有的包都是模块,但不是所有的模块都是包。换句话说,包只是一种特别的模块。任何拥有__path__属性的模块都会被识别包。
任何模块都有一个名称。与标准Python语法相似,子包可以由它的父包加点来访问。(比如Parent.child)因此你可能会又一个模块叫做sys,和一个包叫做email,而email又有一个子包email.mime,子包中的模块叫做email.mime.text。
5.2.1. Regular packages
Python defines two types of packages, regular packages and namespace packages. Regular packages are traditional packages as they existed in Python 3.2 and earlier. A regular package is typically implemented as a directory containing an
__init__.py
file. When a regular package is imported, this__init__.py
file is implicitly executed, and the objects it defines are bound to names in the package’s namespace. The__init__.py
file can contain the same Python code that any other module can contain, and Python will add some additional attributes to the module when it is imported.For example, the following file system layout defines a top level
parent
package with three subpackages:parent/ __init__.py one/ __init__.py two/ __init__.py three/ __init__.pyImporting
parent.one
will implicitly executeparent/__init__.py
andparent/one/__init__.py
. Subsequent imports ofparent.two
orparent.three
will executeparent/two/__init__.py
andparent/three/__init__.py
respectively.
5.2.1. 普通包
Python定义了两种包,普通包与命名空间包。普通包是传统意义上的包,就像Python 3.2或更早版本的一样。普通包通常为包含__init__.py的文件系统目录。当一个普通包被导入引用(import)时,包中的__init__.py文件会立即执行,它定义的对象也会绑定到包中命名空间的名称集合中。__init__.py文件可以包含其他Python模块中同样的代码,并且再被导入时Python会向这个模块添加一些额外的属性。
比如说,下面这个文件系统布局就定义了一个顶层父包(parent)与三个子包:
parent/
__init__.py
one/
__init__.py
two/
__init__.py
three/
__init__.py
导入 parent.one 包会立即执行 parent/__init__.py 和 parent/one/__init__.py。随后导入的 parent.two, parent.three也会执行parent/two/__init__.py 和 parent/three/__init__.py