Python itself has many very useful modules that can be used immediately as soon as the installation is Complete.
Our built-in sys modules, for example, write a hello module:
#!/usr/bin/env python3#-*-coding:utf-8-*- ' a test module ' __author__ = ' Michael Liao ' import sys def test (): args = sys.argv Span class= "keyword" >if len (args) ==1:print ( ' Hello, world! ') elif len (args) ==2:print ( ' Hello,%s! '% args[ 1]) else:print ( ' Too many arguments! ') if __name__== ' __main__ ': test ()
Lines 1th and 2nd are standard comments, and the 1th line of comments allows the hello.py file to run directly on unix/linux/mac, and the 2nd line comment indicates that the. py file itself uses standard UTF-8 encoding;
Line 4th is a string representing the document comment of the module, and the first string of any module code is treated as a document comment of the module;
The 6th line uses __author__ variables to write the author in, so that when you open the source code, others will be able to admire your name;
The above is the Python module standard file template, of course, You can also delete all do not write, but, according to the standard is certainly correct.
The real Code section starts at the BACK.
You may have noticed that the first step in using the sys module is to import the Module:
import sys
After importing the sys module, we have a variable sys pointing to the module, which sys allows access to all the sys functions of the Module.
sysThe module has a argv variable that stores all the parameters of the command line with the List. argvthere is at least one element, because the first parameter is always the name of The. py file, for Example:
python3 hello.pythe operation obtained sys.argv is [‘hello.py‘] ;
python3 hello.py Michaelthe operation obtained sys.argv is [‘hello.py‘, ‘Michael] .
finally, Notice the two lines of code:
if __name__==‘__main__‘: test()
When we run the module file at the command line hello , the Python interpreter places a special variable __name__ __main__ , and if the module is imported elsewhere, the hello if judgment will fail, so this if Testing allows a module to execute some extra code when it runs through the command line, most commonly by running Tests.
We can run with the command line to hello.py see the Effect:
$ python3 hello.pyHello, world!$ python hello.py MichaelHello, Michael!
If you start the Python interactive environment, then import the hello module:
python3Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 23 2015, 02:52:03) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwinType "help", "copyright", "credits" or "license" for more information.>>> import hello>>>
When importing, there is no print Hello, word! , because no function is executed test() .
hello.test()to print out when it is called Hello, word! :
>>> hello.test()Hello, world!
Scope
In a module, we may define many functions and variables, but some functions and variables we want to use for others, some functions and variables we want to use only within the Module. In python, this is done by a _ prefix.
Normal functions and variable names are public and can be referenced directly, such as: abc , x123 , PI etc.;
__xxx__Such variables are special variables, can be directly referenced, but there are special purposes, such as the above __author__ , __name__ is a special variable, hello the module definition of the document can also be accessed with special variables __doc__ , our own variables are generally not used this variable name;
Similar _xxx and __xxx such functions or variables are non-public (private), should not be directly referenced, such as _abc , __abc etc.;
The reason why we say that private functions and variables should not be directly referenced, rather than "cannot" be directly referenced, is because Python does not have a way to completely restrict access to private functions or variables, but from a programming habit should not refer to private functions or variables.
Private functions or variables should not be referenced by others, what is the use of them? Take a look at the example:
def _private_1(name): return ‘Hello, %s‘ % namedef _private_2(name): return ‘Hi, %s‘ % namedef greeting(name): if len(name) > 3: return _private_1(name) else: return _private_2(name)
We expose the function in the module and greeting() hide the internal logic with the private function, so that the calling greeting() function does not care about the internal private function details, which is also a very useful way to encapsulate and abstract the code, namely:
Functions that do not need to be referenced externally are all defined as private, and only functions that need to be referenced externally are defined as Public.
Python's Use Module