模块
一.简介
模块基本上就是一个包含了所有你定义的函数和变量的文件。为了在其他程序中重用模块,模块的文件名必须以.py
为扩展名。
例如:
- #!/usr/bin/python
- # Filename: using_sys.py
- import sys
- print 'The command line arguments are:'
- for i in sys.argv:
- print i
- print '/n/nThe PYTHONPATH is', sys.path, '/n'
输出为:
- $ python using_sys.py we are arguments
- The command line arguments are:
- using_sys.py
- we
- are
- arguments
- The PYTHONPATH is ['/home/swaroop/byte/code', '/usr/lib/python23.zip',
- '/usr/lib/python2.3', '/usr/lib/python2.3/plat-linux2',
- '/usr/lib/python2.3/lib-tk', '/usr/lib/python2.3/lib-dynload',
- '/usr/lib/python2.3/site-packages', '/usr/lib/python2.3/site-packages/gtk-2.0']
二.字节编译的.pyc文件
输入一个模块相对来说是一个比较费时的事情,所以Python做了一些技巧,以便使输入模块更加快一些。一种方法是创建字节编译的文件,这些文件以.pyc
作为扩展名。字节编译的文件与Python变换程序的中间状态有关。当你在下次从别的程序输入这个模块的时候,.pyc
文件是十分有用的——它会快得多,因为一部分输入模块所需的处理已经完成了。另外,这些字节编译的文件也是与平台无关的。
三.from..import语句
如果你想要直接输入argv
变量到你的程序中,那么你可以使用from sys import argv
语句。如果你想要输入所有sys
模块使用的名字,那么你可以使用from sys import *
语句。这对于所有模块都适用。一般说来,应该避免使用from..import
而使用import
语句,因为这样可以使你的程序更加易读,也可以避免名称的冲突。
四.模块的__name__
每个模块都有一个名称,在模块中可以通过语句来找出模块的名称。,当一个模块被第一次输入的时候,这个模块的主块将被运行。假如我们只想在程序本身被使用的时候运行主块,而在它被别的模块输入的时候不运行主块,关于这点,这可以通过模块的__name__属性完成。
- #!/usr/bin/python
- # Filename: using_name.py
- if __name__ == '__main__':
- print 'This program is being run by itself'
- else:
- print 'I am being imported from another module'
输出为:
- $ python using_name.py
- This program is being run by itself
- $ python
- >>> import using_name
- I am being imported from another module
- >>>
每个Python模块都有它的__name__
,如果它是'__main__'
,这说明这个模块被用户单独运行,我们可以进行相应的恰当操作。
五.自己创作模块
例如:
写一个module
- #!/usr/bin/python
- # Filename: mymodule.py
- def sayhi():
- print 'Hi, this is mymodule speaking.'
- version = '0.1'
- # End of mymodule.py
进行使用
- #!/usr/bin/python
- # Filename: mymodule_demo.py
- import mymodule
- mymodule.sayhi()
- print 'Version', mymodule.version
输出为:
- $ python mymodule_demo.py
- Hi, this is mymodule speaking.
- Version 0.1
在这里也可以使用from..import来进行调用
例如:
- #!/usr/bin/python
- # Filename: mymodule_demo2.py
- from mymodule import sayhi, version
- # Alternative:
- # from mymodule import *
- sayhi()
- print 'Version', version
输出相同.
六.dir()函数
你可以使用内建的dir
函数来列出模块定义的标识符。标识符有函数、类和变量。
例如:
- $ python
- >>> import sys
- >>> dir(sys) # get list of attributes for sys module
- ['__displayhook__', '__doc__', '__excepthook__', '__name__', '__stderr__',
- '__stdin__', '__stdout__', '_getframe', 'api_version', 'argv',
- 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats',
- 'copyright', 'displayhook', 'exc_clear', 'exc_info', 'exc_type',
- 'excepthook', 'exec_prefix', 'executable', 'exit', 'getcheckinterval',
- 'getdefaultencoding', 'getdlopenflags', 'getfilesystemencoding',
- 'getrecursionlimit', 'getrefcount', 'hexversion', 'maxint', 'maxunicode',
- 'meta_path','modules', 'path', 'path_hooks', 'path_importer_cache',
- 'platform', 'prefix', 'ps1', 'ps2', 'setcheckinterval', 'setdlopenflags',
- 'setprofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout',
- 'version', 'version_info', 'warnoptions']
- >>> dir() # get list of attributes for current module
- ['__builtins__', '__doc__', '__name__', 'sys']
- >>>
- >>> a = 5 # create a new variable 'a'
- >>> dir()
- ['__builtins__', '__doc__', '__name__', 'a', 'sys']
- >>>
- >>> del a # delete/remove a name
- >>>
- >>> dir()
- ['__builtins__', '__doc__', '__name__', 'sys']
- >>>