Module
I. Introduction
The module is basically a file that contains all the functions and variables you define. To reuse a module in other programs, the module File name must be.py
Is the extension.
For example:
#! /Usr/bin/python <br/> # filename: using_sys.py </P> <p> Import sys </P> <p> Print 'the command line arguments are: '<br/> for I in SYS. argv: <br/> print I </P> <p> Print '\ n \ nthe pythonpath is', sys. path, '\ N'
Output:
$ Python using_sys.py We Are arguments <br/> the command line arguments are: <br/> using_sys.py <br/> WE <br/> are <br/> arguments </P> <p> the pythonpath is ['/home/swaroop/byte/code ', '/usr/lib/python23.zip', <br/> '/usr/lib/python2.3', '/usr/lib/python2.3/plat-linux2 ', <br/> '/usr/lib/python2.3/lib-TK', '/usr/lib/python2.3/lib-dynload ', <br/> '/usr/lib/python2.3/Site-packages', '/usr/lib/python2.3/Site-packages/gtk-2.0'] </P> <p>
Ii. byte-compiled. PyC File
Inputting a module is a relatively time-consuming task. Therefore, Python provides some tips to accelerate the input module. One way is to create byte-compiled files.pyc
As the extension. Byte-compiled files are related to the intermediate state of the python converter. When you input this module from another program next time,.pyc
File is very useful-it will be much faster, because the processing required by some input modules has been completed. In addition, the files compiled by these bytes are not related to the platform.
Iii. From... Import Statement
If you want to directly enterargv
You can usefrom sys import argv
Statement. If you want to enter allsys
Module name, you can usefrom sys import *
Statement. This applies to all modules. Generally, avoid usingfrom..import
And useimport
Statement, because this can make your program easier to read and avoid name conflicts.
4. Module _ name __
Each module has a name. In the module, you can use statements to find the module name ., When a module is input for the first time, the main module of this module is run. If we only want to run the main block when the program itself is used, but do not run the main block when it is input by other modules, this can be done through the _ name _ attribute of the module.
#! /Usr/bin/python <br/> # filename: using_name.py </P> <p> If _ name _ = '_ main __': <br/> Print 'this program is being run by itself-service '<br/> else: <br/> Print 'I am being imported from another module' </P> <p>
Output:
$ Python using_name.py <br/> this program is being run by itself </P> <p> $ python <br/> Import using_name <br/> I am being imported from another module <br/>>></P> <p>
Each Python module has its own__name__
If it is'__main__'
This indicates that this module is run by the user separately and we can perform appropriate operations.
5. Self-creation Module
For example:
Write a module
#! /Usr/bin/python <br/> # filename: mymodule. PY </P> <p> def sayhi (): <br/> Print 'Hi, this is mymodule speaking. '</P> <p> Version = '0. 1' </P> <p> # End of mymodule. PY </P> <p>
For use
#! /Usr/bin/python <br/> # filename: mymodule_demo.py </P> <p> Import mymodule </P> <p> mymodule. sayhi () <br/> Print 'version', mymodule. version </P> <p>
Output:
$ Python mymodule_demo.py <br/> Hi, this is mymodule speaking. <br/> version 0.1 </P> <p>
You can also use from .. import to call
For example:
#! /Usr/bin/python <br/> # filename: mymodule_demo2.py </P> <p> from mymodule import sayhi, version <br/> # alternative: <br/> # From mymodule import * </P> <p> sayhi () <br/> Print 'version', version
The output is the same.
Vi. dir () function
You can use the built-indir
Function to list the identifier defined by the module. Identifiers include functions, classes, and variables.
For example:
$ Python <br/>>>> import sys <br/> Dir (sys) # Get list of attributes for SYS module <br/> ['_ displayhook _', '_ Doc _', '_ thook __', '_ name _', '_ stderr _', <br/> '_ stdin _', '_ stdout __', '_ getframe', 'api _ version', 'argv', <br/> 'builtin _ module_names ', 'byteorder', 'Call _ tracing', 'callstats ', <br/> 'copyright', 'displayhook', 'exc _ clear', 'exc _ info', 'exc _ type', <br/> 'mongothook ', 'exec _ prefix', 'executable', 'exit ', 'getcheckinterval', <br/> 'getdefaultencoding', 'getdlopenflags ', 'getfilesystemencoding ', <br/> 'getcursionlimit ', 'getrefcount', 'hversion', 'maxint', 'maxunicode', <br/> 'meta _ path', 'modules ', 'path', 'path _ hooks ', 'path _ importer_cache', <br/> 'platform', 'prefix', 'ps1', 'ps2 ', 'setcheckinterval ', 'setdlopenflags ', <br/> 'setprofile', 'setrecursionlimit ', 'settrack', 'stderr', 'stdin', 'stdout', <br/> 'version ', 'version _ info', 'waroptions'] <br/> Dir () # Get list of attributes for current module <br/> ['_ builtins _', '_ Doc _', '_ name __', 'sys '] <br/> A = 5 # create a new variable 'A' <br/> Dir () <br/> ['_ builtins _', '_ Doc _', '_ name _', 'A ', 'sys '] <br/> Del a # delete/remove a name <br/> dir () <br/> ['_ builtins _', '_ Doc _', '_ name __', 'sys '] <br/> </P> <p>