Excerpt: Https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/ 001431845183474e20ee7e7828b47f7b7607f2dc1e90dbb000
This article is completely used for personal review, invasion and deletion;
Our built-in sys modules, for example, write a hello module:
#!/usr/bin/env Python3#-*-coding:utf-8-*-'a test module'__author__='Michael Liao'ImportSYSdefTest (): args=SYS.ARGVifLen (args) ==1: Print('Hello, world!.') elifLen (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.
The sys module has a argv variable that stores all the parameters of the command line with the list. argv There 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 hello module file at the command line , the Python interpreter places a special __name__ __main__ variable , and if the module is imported elsewhere hello , the if judgment will fail, so This kind of if test allows a module to execute some extra code from the command line runtime, most commonly running tests.
We can run with the command line to hello.py see the effect:
$ python3 Hello.pyhello, world! $ python3 hello.py Michaelhello, michael!
If you start the Python interactive environment, then import the hello module:
Import Hello>>>
When importing, there is no print Hello, word! , because no function is executed test() .
is called to hello.test() print out theHello, 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.;
a __xxx__ variable like this is a special variable that can be referenced directly , but has a special purpose, such as the above __author__ , __name__ A special variable, hello module-defined document annotations can also be accessed with special variables __doc__ , our own variables are generally not used in 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 , however, The private function or variable should not be referenced from the programming habit.
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'%namedefGreeting (name):ifLen (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 Learning Notes (12) using modules