Talking about the modules in Python

Module

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.

Use modules

! /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

Scope

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

Talking about the modules in Python
Talking about inheritance in Python
Talking about strings in Python
Talking about Python coroutine
Talking about Python functional programming
The usage of wheel in python
Talking about php calling python files
​What are the numbers in Python?
The usage of tuples in python
Understanding the meaning of rb in python
How to introduce third-party modules in Python
The usage of Ajax in Python3 crawler
How to install third-party modules in Python
How to add custom modules in Python
How to view installed modules in python
Python randomly shuffles the elements in the list
Talking about Python multithreading and program lock
Functions in python
2.1 The Python Interpreter (python interpreter)
What is the function of adb in python
How to use the round function in python
How to use the zip function in Python
What does the tab key in python mean
Python implements the shuffling of the cards in Doudizhu
Talking about the first experience of using Ubuntu
How to use the format function in python
First acquainted with python, the grammatical rules in python
How about learning python at the age of 27?
The best way to judge the type in Python
03. Operators in Python entry
Join function in Python
12. Network Programming in Python3
print statement in python
How to understand the introduction of packages in Python
Concurrent requests in Python
Install python in Ubuntu
Learn about Python3 coroutine
Consolidate the Python foundation (2)
Context management in Python
Arithmetic operators in python
python download new modules
Python data visualization: who are the big names in Python?
Write gui in python
MongoDB usage in Python
09. Common modules of Python3
Str string in Python
For the first time in history, Python surpassed Java!
Computational Geometry in Python
What are the ways to open files in python
How to find the area of a circle in python
Learn the hard core operation of Python in one minute
Concurrent requests in Python (part 2)
Consolidate the foundation of Python (4)
python guess the word game
Consolidate the foundation of Python(6)
Ubuntu install the latest Python 3.
Python realizes the guessing game
Noteworthy update points in Python 3.9
Consolidate the foundation of Python(5)
Containerize Python applications in 3 minutes
python functions, classes, modules, packages