In order to write maintainable code, we group many functions into different files. In this way, each file contains relatively little code. Many programming languages use this way of organizing codes. In Python, a .py file is called a module (Module).
**What are the benefits of using modules? **
When a module is written, it can be referenced elsewhere. When we write programs, we often refer to other modules, including built-in Python modules and modules from third parties.
Modules can also avoid conflicts between function names and variable names. Functions and variables with the same name can be stored in different modules. But also pay attention, try not to conflict with built-in function names.
What if the module names written by different people are the same? In order to avoid module name conflicts, Python has introduced a method of organizing modules by directories, which is called Package.
Assuming that the names of our abc and xyz modules conflict with other modules, we can organize the modules through packages to avoid conflicts. The method is to choose a top-level package name, such as mycompany, and store it in the following directory:
After the package is introduced, as long as the top-level package name does not conflict with others, all modules will not conflict with others. Now, the name of the abc.py module has become mycompany.abc, and similarly, the module name of xyz.py has become mycompany.xyz.
Please note that there will be an init.py file under each package directory. This file must exist, otherwise, Python will treat this directory as a normal directory, not a package. init.py can be an empty file or Python code, because init.py itself is a module, and its module name is mycompany.
! /usr/bin/env python3
# - *- coding: utf-8-*-' a test module '
__ author__ ='Michael Liao'import sys
def test():
args = sys.argv
iflen(args)==1:print('Hello, world!')
elif len(args)==2:print('Hello, %s!'% args[1])else:print('Too many arguments!')if __name__=='__main__':test()
The first and second lines are standard comments. The first line of comments allows this hello.py file to run directly on Unix/Linux/Mac. The second line of comments indicates that the .py file itself uses standard UTF-8 encoding;
Line 4 is a string that represents the documentation comment of the module. The first string of any module code is regarded as the documentation comment of the module;
Line 6 uses the author variable to write the author, so that when you publish the source code, others can look at your name;
The above is the standard file template for Python modules
The first step to use the sys module is to import the module import sys
. After importing the sys module, we have the variable sys pointing to the module. Using the sys variable, we can access all the functions of the sys module.
The sys module has an argv variable, and the list stores all the parameters entered when using the module on the command line. argv has at least one element, because the first parameter is always the name of the .py file, for example:
Run python3 hello.sys obtained by py.argv is['hello.py'];
Run python3 hello.sys obtained by py Michael.argv is['hello.py', 'Michael]。
The tool script is to read the use parameters through sys.argv
Finally, notice these two lines of code
if __name__=='__main__':test()
When we run the hello module file on the command line, the Python interpreter sets a special variable name to main, and if the hello module is imported elsewhere, name is elsewhere, and the if judgment will fail, so , This kind of if test allows a module to execute some additional code when running through the command line, the most common is to run the test
such as:
If you use python3 hello.py directly from the command line, __name__
is __main__
, and if meets the requirements, execute test()
If you import and use the hello.py module from test.py, __name__
will be test. If if is not satisfied, test() will not be executed
In a module, we may define many functions and variables, but some functions and variables we want to use for others, and some functions and variables we want to use only inside the module. In Python, this is achieved through the _ prefix.
Normal function and variable names are public and can be directly quoted, such as: abc, x123, PI, etc.;
Variables like xxx are special variables that can be directly quoted, but have special uses. For example, the above author and name are special variables. The documentation comments defined by the hello module can also be accessed with the special variable doc. We Do not use this variable name for your own variables;
Functions or variables like _xxx and __xxx are non-public (private) and should not be directly quoted, 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, it is not a programming practice. Should refer to private functions or variables.
In other words, it's just a declaration, it can still be accessed if you want to access it. Private functions or variables shouldn't be referenced by others, so what use are they?
ef _private_1(name):return'Hello, %s'% name
def _private_2(name):return'Hi, %s'% name
def greeting(name):iflen(name)3:return_private_1(name)else:return_private_2(name)
We expose the greeting() function in the module, and hide the internal logic with a private function, so that calling the greeting() function does not need to care about the details of the internal private function. This is also a very useful method of code encapsulation and abstraction. which is:
**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. **
The above is the detailed content of the module in Python. For more information about the python module, please pay attention to other related articles on ZaLou.Cn!
Recommended Posts