您的位置:首页 > 编程语言 > Python开发

Python 语言及其应用 Chapter_5_Note_1 模块

2016-07-03 22:28 549 查看
导入模块

import 语句最简单的用法是import 模块,模块是不带.py 扩展的另外一个Python 文件的

文件名。现在来模拟一个气象站,并输出天气预报。其中一个主程序输出报告,一个单独

的具有单个函数的模块返回天气的描述。
下面是主程序(命名为weatherman.py):

import report

description = report.get_description()

print("Today's weather:", description)
以下是天气模块的代码(report.py):

def get_description(): #看到下面的文档字符串了吗?

"""Return random weather, just like the pros"""

from random import choice

possibilities = ['rain', 'snow', 'sleet', 'fog', 'sun', 'who knows']

return choice(possibilities)

如果上述两个文件在同一个目录下,通过Python 运行主程序weatherman.py,会引用

report 模块,执行函数get_description()。函数get_description() 从字符串列表中返回

一个随机结果。下面就是主程序可能返回和输出的结果:

$ python weatherman.py

Today's weather: who knows

$ python weatherman.py

Today's weather: sun

$ python weatherman.py

Today's weather: sleet

我们在两个不同的地方使用了import:

• 主程序weatherman.py 导入模块report;

• 在模块文件report.py 中,函数get_description() 从Python 标准模块random 导入函数

choice。

同样,我们以两种不同的方式使用了import:

• 主程序调用import report,然后运行report.get_description();

• report.py 中的get_description() 函数调用from random import choice, 然后运行

choice(possibilities)。

第一种情况下,我们导入了整个report 模块,但是需要把report. 作为get_description()

的前缀。在这个import 语句之后,只要在名称前加report.,report.py 的所有内容(代码

和变量)就会对主程序可见。通过模块名称限定模块的内容,可以避免命名冲突。其他模

块可能也有函数get_description(),这样做不会被错误地调用。

第二种情况下,所有代码都在同一个函数下,并且没有其他名为choice 的函数,所以我

们直接从random 模块导入函数choice()。我们也可以编写类似于下面的函数,返回随机

结果:

def get_description():

import random

possibilities = ['rain', 'snow', 'sleet', fog', 'sun', 'who knows']

return random.choice(possibilities)

同编程的其他方面一样,选择你所能理解的最清晰的风格。符合模块规范的命名(random.

choice)更安全,但输入量略大。

这些get_description() 的例子介绍了各种各样的导入内容,但没有涉及在什么地方进行

导入——它们都在函数内部调用import。我们也可以在函数外部导入random:

>>> import random

>>> def get_description():

... possibilities = ['rain', 'snow', 'sleet', 'fog', 'sun', 'who knows']

... return random.choice(possibilities)

...

>>> get_description()

'who knows'

>>> get_description()

'rain'

如果被导入的代码被多次使用,就应该考虑在函数外部导入;如果被导入的代码使用有

限,就在函数内部导入。一些人更喜欢把所有的import 都放在文件的开头,从而使代码之

间的依赖关系清晰。两种方法都是可行的。

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

这里补一下关于choice函数的使用说明

import random

random.choice( seq )

注意:choice()是不能直接访问的需要导入 random 模块,然后通过 random 静态对象调用该方法。
seq -- 可以是一个列表,元组或字符串

import random

print "choice([1, 2, 3, 5, 9]) : ", random.choice([1, 2, 3, 5, 9])

print "choice('A String') : ", random.choice('A String')

以上实例运行后输出结果为:

choice([1, 2, 3, 5, 9]) :  2

choice('A String') :  n

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

使用别名导入模块

在主程序weatherman.py 中,我们调用了import report。但是,如果存在同名的另一个模

块或者你想使用更短更好记的名字,该如何做呢?在这种情况下,可以使用别名wr 进行

导入:

import report as wr

description = wr.get_description()

print("Today's weather:", description)

导入模块的一部分

在Python 中,可以导入一个模块的若干部分。每一部分都有自己的原始名字或者你起的别

名。首先,从report 模块中用原始名字导入函数get_description():

from report import get_description

description = get_description()

print("Today's weather:", description)

用它的别名do_it 导入:

from report import get_description as do_it

description = do_it()

print("Today's weather:", description)

模块搜索路径

Python 会在什么地方寻找文件来导入模块?使用命名为path 变量的存储在标准sys 模块

下的一系列目录名和ZIP 压缩文件。你可以读取和修改这个列表。下面是在我的Mac 上

Python 3.3 的sys.path 的内容:

>>> import sys

>>> for place in sys.path:

... print(place)

...

/Library/Frameworks/Python.framework/Versions/3.3/lib/python33.zip

/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3

/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/plat-darwin

/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/lib-dynload

/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages

最开始的空白输出行是空字符串'',代表当前目录。如果空字符串是在sys.path 的开始

位置,Python 会先搜索当前目录:import report 会寻找文件report.py。

第一个匹配到的模块会先被使用,这也就意味着如果你在标准库之前的搜索路径上定义一

个模块random,就不会导入标准库中的random 模块。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python 模块