掌握点:

模块功能
导入方式及使用区别
模块定位
包
常用函数

模块功能

将代码分配到不同模块中,逻辑条理更清晰,便于重用。 简单来说,模块就是一个保存了python代码的文件。模块能定义函数,类和变量。也能包含可执行的代码。

例:创建一个模块 support.py

def print_func(par):
    print 'Hello :',par
    return

def print_test():
    print 'this is a test'
    return

导入方式及使用区别

1. 直接导入
import support

使用如下:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

注:当解释器遇到import语句,如果模块在当前的搜索路径就会被导入,搜索路径是一个解释器会先进行搜索的所有目录的列表。 无论执行多少次import,一个模块只会被导入一次。

import support  #导入模块
support.print_func('zara,你好啊')
2. 导入模块中部分功能
from support import print_func

使用如下:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
from support import print_func
print_func('zara,你好啊')      #区别与上个,不需要使用support调用了,可以直接使用print_fun函数了
注: 区别与import是可以直接进行使用,不过不容易区分出当前使用的方法属于哪一模块

3. 导入模块所有功能,可以直接使用模块中所有函数
from support import *

使用如下:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
from support import *
print_func('zara,你好啊')      #可直接使用
print_Num()         #可直接使用

模块定位

使用模块,解释器一定要先能定位到模块位置才可以,解释器对模块位置的搜索顺序为:

1 . 当前目录(当前执行文件所在目录)

2 . 如下不在当前目录,python则搜索在shell变量PYTHONPATH下的每个目录。

3 . 如果都找不到,python会察看默认路径。UNIX下,默认路径一般为/usr/local/lib/python/

Python包

包是一个分层次的文件目录结构,它定义了一个由模块及子包和子包下子包等组成的python的应用环境。

假定,你有一个目录为pythonTest,在这个目录下有三个文件,test1.py,test2.py,test3.py这三个文件,每个文件有各有几个执行函数,现在想将这三个文件组装成一个文件;

目标是:只导入一个文件,便可直接使用其中的函数。
例:
test1.py:

def print_test1():
    print 'this is a test1 file’

test2.py

def print_test2():
    print 'this is a test2 file’

test3.py:

def print_test3():
    print 'this is a test3 file’

写第4个文件:test4.py

from test1 import print_test1   #注,没有小括号,是按功能导入,不是函数
from test2 import print_test2
from test3 import print_test3

使用:我们创建test文件来导入test4即可:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import test4

#直接使用test1,test2,test3文件中的函数
test4.print_test1()
test4.print_test2()
test4.print_test3()

注: 以上为一个小例子,完全可以定义许多类来方便使用,后续只需要使用包名(test4)来直接调用即可

获取模块中内容

1 . dir():返回一个排好序的字符串列表,内容是一个模块里定义的所有模块,变量和函数
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import math
#print 'math', dir(math)

输出:

['__doc__', '__file__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']
2 . globals()和locals()函数:

根据调用地方不同,global()locals()函数用来返回全局和局部命名空间里的名字。

如果在函数内部调用locals()   :返回的是所有能在该函数里访问的命名
如果在函数内部调用globals() :返回的是所有在该函数里能访问的全局名字

在全局调用两个方法返回相同结果

注:返回结果都为字典,所以名字能用kyes()函数操作
如:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
index = 10
def updateIndex():
    global index
    index += 1
    a = 10
    print 'locals :',locals(),'\n'
    print 'globals :',globals() ,'\n'
    return

#print index
updateIndex()
#print index
print 'locals :',locals(), '\n'  #与下面打印一样
print 'globals :',globals(), '\n'

输出:

locals : {'a': 10}  #函数内部局部变量

globals : {'index': 11, '__builtins__': <module '__builtin__' (built-in)>, '__file__': './test.py', '__package__': None, 'updateIndex': <function updateIndex at 0x104a91c08>, '__name__': '__main__', '__doc__': None} #函数内部可访问的全局变量

locals : {'index': 11, '__builtins__': <module '__builtin__' (built-in)>, '__file__': './test.py', '__package__': None, 'updateIndex': <function updateIndex at 0x104a91c08>, '__name__': '__main__', '__doc__': None}  #全局情况

globals : {'index': 11, '__builtins__': <module '__builtin__' (built-in)>, '__file__': './test.py', '__package__': None, 'updateIndex': <function updateIndex at 0x104a91c08>, '__name__': '__main__', '__doc__': None}  #全局情况
3 . reload()函数

当一个模块被导入一个脚本,模块顶层部分的代码只会执行一次,如果想重新执行模块里顶层部分代码,可以用reload()函数。

格式:
reload(模块名)