python learning [module]

Preface

In the previous article python learning [Deep Copy], I learned about the deep and shallow copy learning content in python. This article will continue to learnmodules in python

what is module

In python, a file (a file with the suffix " .py ") is called a module, and each module is regarded as an independent file in python.

A module can be used by other modules in the project, some scripts, and even interactive parsers. It can be referenced by other programs to use functions and other functions in the module. This method is also used when using the standard library in Python.

Common system built-in modules in python are:

os module : The os module contains common operating system functions.
The sys module : Provides a series of variables and functions related to the Python operating environment.
The random module : The random module is used to generate random numbers.
The time module : Mainly contains various classes that provide date and time functions. and function
datetime module : an advanced encapsulation of the time module
shutil module : a high-level file operation tool
logging module : prints the log to the standard output
re module : can be called directly to achieve regular matching
pymysql module : connect to the database, And implement simple addition, deletion, modification and query
threading module : provides a more powerful multi-thread management solution
queue module : implements multi-producer, multi-consumer queue
json module : used to convert json between strings and data types

Import of modules

The methods of importing modules in python are:

import  模块名称  [as别名]
from  模块名称 import  函数/变量/类

for example:

import module name [as alias]

# 例子1  导入数学运算模块  math
import math
print(math,id(math),type(math))
print(math.pi)  #返回一个浮点数 3.141592653589793,表示圆周率。
print('------------------进行math模块中相关函数的使用-----------/')
print(dir(math))
print('pow()函数的使用',math.pow(2,3),type(math.pow(2,3)))  #2的3次方
print('向上取整',math.ceil(3.00001))  #向上取整  4
print('向下取整',math.floor(3.99999))   #向下取整3

Insert image description here

from module name import function/variable/class

# 只引入math模块中的pi
from math  import  pi
print(pi)   #3.141592653589793

When we use this method to introduce a module, we must pay attention to the fact that we are importing the pi constant in the module. If we introduce other functions in the math module at this time, the program will report an error:

Insert image description here

from math  import  pi
# 尝试引入pow()函数
print(pow(2,3),type(pow(2,3)))  #8 <class 'int'> 

We see that when we try to introduce the math function that has not been imported, we will find that there is also a return value; but the pow() function here is not a function in the math module, and its output type is int type instead of pow in the math module. The output type of the function is float, so although the return value appears, it does not call the pow() function in the math module.

Insert image description here

Custom module

After understanding what a module is in python, we can customize the module:

The custom module names the python file to a concise and meaningful file name.

Insert image description here

定义了一个cal模块
def  add(a,b):
    return  a+b
def  div(a,b):
    return a/b

Then import the custom module:

# 引入新建的cal模块
import  cal
print(cal.add(10,30))
print(cal.div(10,20))

# 只导入cal模块中的add函数
from cal import add
print(add(10,20))

Insert image description here

Run the main program

在每个模块的定义中都包括一个模块名称的变量 __name__ ,变量的值是__main__。Programs can examine this variable to determine in which module they are executing; if a module is not imported into another program, it may be executing in the top-level module of the interpreter.

We customize a module cal2:
Insert image description here
import it in index1.py:
Insert image description here
We find that when we run the file of index1, the result of the operation is that cal2 is also run, which means that after cal2 is imported by index1, it will be run when index1 Also run, what if we want to specify a program to run?

This requires adding the main program running code in the cal2 module:

def  add(a,b):
    return a+b
# 直接运行导入cal2的指定文件   只有点击运行cal2这个文件时才能运行如下语句
if __name__ =='__main__':
    print(add(10,30))

Insert image description here
This will prevent the module from running at the same time as the new file after it is imported.

One sentence per article

Learning is a lifelong ability.
If there are any deficiencies, thank you for correcting me!

Guess you like

Origin blog.csdn.net/weixin_64122448/article/details/132925058