模块
模块,module,一个.py(.pyw)源文件就是一个module,不同module内变量名可重复,不冲突
注:尽量不要与内置函数名冲突
应用
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'arith module'
__author__ = 'mardax'
def add(a, b):
print a, '+', b, '=', a + b
def sub(a, b):
print a, '-', b, '=', a - b
if __name__ == '__main__':
add(18, 8)
sub(18, 8)
解释:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'arith module'
__author__ = 'mardax'
python源文件标准文件模板,分别表示:
- .py文件允许在Unix/Linux/Mac上运行,只要.py文件有执行权限
- .py文件本身使用标准UTF-8编码
- .py文件文档注释,可通过__doc__访问,即无论是否显式定义__doc__,__doc__都存在
- .py文件作者
注:标准文件模板可全部删除不写,但遵循标准肯定不是坏事
if __name__ == '__main__':
add(18, 8)
sub(18, 8)
注:如果直接从当前.py文件开始执行,__name__会被赋值为'__main__',如果被其他.py文件引用执行,__name__被赋值为模块全路径名(包括包路径,不包括模块文件后缀名),可利用此特性进行模块代码自测
包
包,package,为避免模块名字冲突,python引入包概念,按目录来组织管理模块,类似于java的package
在包目录组织结构里,包目录也必须是模块,为区别普通目录,包目录下必须包含一个__init__.py源文件,代表包目录对应模块
应用
目录math,math下包含3个.py文件:
- __init__.py
- arith.py
- cmp.py
__init__.py:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'math module'
__author__ = 'mardax'
def info():
print "arith:"
print ['add', 'sub']
print "cmp:"
print ['gt', 'lt']
if __name__ == '__main__':
info()
注:__init__.py即为包目录math对应模块
arith.py:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'arith module'
__author__ = 'mardax'
def add(a, b):
print a, '+', b, '=', a + b
def sub(a, b):
print a, '-', b, '=', a - b
if __name__ == '__main__':
add(18, 8)
sub(18, 8)
cmp.py:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'cmp module'
__author__ = 'mardax'
def gt(a, b):
print 'gt(%d, %d) = %d' %(a, b, a > b)
def lt(a, b):
print 'lt(%d, %d) = %d' %(a, b, a < b)
if __name__ == '__main__':
gt(18, 8)
lt(18, 8)
调用:
import math.arith
import math.cmp
math.info()
math.arith.add(18, 8)
math.arith.sub(18, 8)
math.cmp.gt(18, 8)
math.cmp.lt(18, 8)
output:
arith:
['add', 'sub']
cmp:
['gt', 'lt']
18 + 8 = 26
18 - 8 = 10
gt(18, 8) = 1
lt(18, 8) = 0
别名
import...as
import math.arith as arith
arith.add(18, 8)
arith.sub(18, 8)
等价于:
import math.arith
arith = math.arith
arith.add(18, 8)
arith.sub(18, 8)
总结:
- import之后必须是module名,本质就是为module名取别名,可用于冗长module名缩写
from...import
from math.arith import add
from math.arith import sub
add(18, 8)
sub(18, 8)
等价于:
import math.arith
add = math.arith.add
import math.arith
sub = math.arith.sub
add(18, 8)
sub(18, 8)
总结:
- from之后必须是module名,import之后必须是module内变量名,本质就是为module内变量名取别名,可用于冗长变量名缩写
模块访问权限
private指只能被模块内部引用,public指可以被模块外部引用,从语法角度,模块的变量名都是public的,都可被模块外部引用
模块没有提供对变量名的访问权限控制机制,模块的访问权限控制是一种自律机制,是一种约定俗称,约定俗称规则如下:
- 正常变量名都是public的,可被直接引用,如abc,xyz
- 形似__xxx__变量名是特殊变量(以__前导,以__后缀),可被直接引用,但有特殊用途,如__author__,__name__
- 形似_xxx变量名(以_前导且非形似__xxx__)是private的,不应被直接引用,如_abc,__xyz
注:private是“不应被直接引用”,而非“不能被直接引用”
math目录下增加文件circle.py:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'circle module'
__author__ = 'mardax'
_PI = 3.14
def length(r):
print 'length(%.1f) = %.1f' %(r, 2 * _PI * r)
def area(r):
print 'area(%.1f) = %.1f' %(r, _PI * r * r)
if __name__ == '__main__':
length(5)
area(5)
调用:
import math.circle as circle
print circle.__author__
print circle.__doc__
print circle.__name__
print circle._PI
circle.length(5)
circle.area(5)
output:
mardax
circle module
math.circle
3.14
length(5.0) = 31.4
area(5.0) = 78.5
注:__name__为模块全路径名,包括包目录结构,不包括模块文件后缀名
第三方模块安装
第三方模块安装工具:
- easy_install
- pip
注:官方推荐使用pip
模块搜索路径
默认情况,python解释器会搜索当前目录,所有已安装内置模块,第三方模块
模块搜索路径存放在sys模块path变量中
修改模块搜索路径方法:
- 运行时修改sys.path,程序运行结束,修改失效
运行时修改sys.path:
import sys
print "before modify:"
print sys.path
sys.path.append('/Users/mac/Desktop/work/python/math')
print "after modify:"
print sys.path