Python基础入门----包

Python Package

In this article, you'll learn to divide your code base into clean, efficient modules using Python packages. Also, you'll learn to import and use your own or third party packagesin your Python program.

Table of Contents

Python包

在这篇文章里,你将学习使用Python包把你代码分开变清晰,模块高效使用。还有,你将学习到在你的Python程序导入和使用你的或第三方包。

表格内容

  • 什么是包?
  • 从一个包导入模块

What are packages?

We don't usually store all of our files in our computer in the same location. We use a well-organized hierarchy of directories for easier access.

Similar files are kept in the same directory, for example, we may keep all the songs in the "music" directory. Analogous to this, Python has packages for directories and modules for files.

As our application program grows larger in size with a lot of modules, we place similar modules in one package and different modules in different packages. This makes a project (program) easy to manage and conceptually clear.

Similar, as a directory can contain sub-directories and files, a Python package can have sub-packages and modules.

A directory must contain a file named __init__.py in order for Python to consider it as a package. This file can be left empty but we generally place the initialization code for that package in this file.

什么是包?

我们通常不会把我们所有的文件存储在我们的电脑的同一位置。我们使用良好组织目录层次为了更好访问。

相似的文件保存在同一个文件夹,例如,我们保存所有的个在music文件夹。与此类似的,Python有用于目录的包和用于模块文件。

随着我们程序变大会有很多模块,我们可以放置相似的模块在一个包里,不同的模块在不带的包。这使工程(程序)变得容易管理和概念清晰。

同样的,一个目录包含了子目录和文件,一个Python包能有子包和模块。

一个目录必须包含文件名为__init__.py为了Python去思考它是一个包。这文件可以是空的,但是我们一般放置初始代码到这个包的文件里。


Here is an example. Suppose we are developing a game, one possible organization of packages and modules could be as shown in the figure below.

这里有一个例子,假设我们正在开发一个游戏,一个可能的包和模块展示在下面。

Package Module Structure in Python Programming


Importing module from a package

We can import modules from packages using the dot (.) operator.

For example, if want to import the start module in the above example, it is done as follows.

import Game.Level.start

从一个包里导模块

我们可以使用点(.)符号从一个包里边导入模块。

例如:在上面例子想导入一个start模块,它的做法如下。

import Game.Level.start

Now if this module contains a function named select_difficulty(), we must use the full name to reference it.

Game.Level.start.select_difficulty(2)

现在如果这模块包含一个函数名为 select_difficulty(),我们可以使用完整名字引用它。

Game.Level.start.select_difficulty(2)

If this construct seems lengthy, we can import the module without the package prefix as follows.

from Game.Level import start

如果这几个结构看起来很长,我们可以导入模块不使用包的前缀,如下。

from Game.Level import start

We can now call the function simply as follows.

start.select_difficulty(2)

我们可以简单调用这函数,如下。 

start.select_difficulty(2)

Yet another way of importing just the required function (or class or variable) form a module within a package would be as follows.

from Game.Level.start import select_difficulty

 导入的另外一种方式仅仅要求从包内导入一个模块的函数(或类或变量),如下。

from Game.Level.start import select_difficulty

Now we can directly call this function.

select_difficulty(2)

Although easier, this method is not recommended. Using the full namespace avoids confusion and prevents two same identifier names from colliding.

While importing packages, Python looks in the list of directories defined in sys.path, similar as for module search path.

现在我们可以直接调用函数。

select_difficulty(2)

尽管很容易,这方法不推荐,使用完整的名称避免混淆,阻止2个相同的特征名碰撞。

正导入包时,Python查看在sys.path里定义的目录列表,类似于模块搜索路径。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值