1.模块
模块是python组织代码的基本方式。python的脚本都是以.py为扩展名保存的文本文件。一个脚本可以单独运行,也可以导入另一个脚本中运行。当脚本被导入运行时,我们将其
称为模块。
模块名与脚本的文件名相同!例如我们编写一个cal.py的脚本,则可以在另外一个脚本中用import cal语句来导入它进行相关运算操作。
user1@ubuntu:~$ cd csvtpy/ user1@ubuntu:~/csvtpy$ ls 10.py 9.py cal.py fabs.py __init__.pyc scal.py 8.py calnew.py cal.pyc __init__.py new.py test.py user1@ubuntu:~/csvtpy$ vim cal.py user1@ubuntu:~/csvtpy$ python cal.py 6 user1@ubuntu:~/csvtpy$ vim calnew.py user1@ubuntu:~/csvtpy$ python calnew.py 7
#!/usr/bin/python #coding:utf8 from __future__ import division def jia(x,y): return x+y def jian(x,y): return x-y def cheng(x,y): return x*y def chu(x,y): return x/y def operator(x,o,y): if o == "+": print jia(x,y) elif o == "-": print jian(x,y) elif o == "*": print cheng(x,y) elif o == "/": print chu(x,y) else: pass if __name__ == "__main__":#该脚本模块默认输入名称__main__ operator(2,"*",3)calnew.py:
#coding:utf8 #允许输入中文 import cal #调用cal模块 x = cal.jia(3,4)#调用cal模块的加法运算 print x
user1@ubuntu:~/csvtpy$ touch __init__.py user1@ubuntu:~/csvtpy$ cd ~ user1@ubuntu:~$ ls csvtpy Documents examples.desktop packtest.py Public test.py Desktop Downloads Music Pictures Templates Videos user1@ubuntu:~$ vim packtest.py user1@ubuntu:~$ python packtest.py 15packtest.py:
#coding:utf8 import csvtpy.cal #调用csvtpy包里面的cal模块 x = csvtpy.cal.jia(7,8) #调用csvtpy包里cal模块的加法运算 print x #打印输出运算结果x
user1@ubuntu:~$ cd csvtpy/ user1@ubuntu:~/csvtpy$ ls 10.py 9.py cal.pyc __init__.py new.py test.py 8.py cal.py fabs.py __init__.pyc scal.py user1@ubuntu:~/csvtpy$ python Python 2.7.3 (default, Sep 26 2013, 20:03:06) [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import cal >>> >>> cal.jia(10,20) 30 >>> import cal as c >>> c.jia(1,2) 3 >>> from cal import jia >>> >>> jia(1,2) 3 >>>