Python Package文档部分翻译

学校的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 called email, which in turn has a subpackage called email.mime and a module within that subpackage called email.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__.py

Importing parent.one will implicitly execute parent/__init__.py and parent/one/__init__.py. Subsequent imports of parent.two or parent.three will execute parent/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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值