A brief understanding of python module concepts and python module concepts
This article describes the concept of a module in Python, as follows.
The module is the basic way for python to organize code:
All python scripts are saved using text files with the extension py.
A script can be run independently or imported to another script.
When a script is imported into another script, it is called a module.
The module name is the same as the script file name:
For example, you have an items. py script,
You can use the import items statement to import it in another script.
This is a piece of python code named cal. py. It will be used as the code after the cal module is imported:
#!/usr/bin/python#coding:utf-8from __future__ import divisiondef jia(x,y): return x+ydef jian(x,y): return x-ydef cheng(x,y): return x*ydef chu(x,y): return x/ydef operator(x,o,y): if o == "+": print jia(x,y) elif o == "-": print jian(x,y) elif o == "*": print cheng(x,y) elif o == "/": print chu(x,y) else: passif __name__=="__main__": operator(2,'+',4)
There are three ways to import the cal module:
#import cal#print cal.jia(1,2)#import cal as c#print c.jia(1,2)from cal import jiaprint jia(1,2)
Another type of import module is a package. It is often used when many modules need to be managed under the same package:
First, create a _ init _. py file in the package where the module code is stored (the folder name is test), and double underscores (_ init _. py) before and after init. Then, you can use the following code in other directories:
import test.calcal.jia(1,2)
Summary:
· The module is a python script file that can be imported;
· A package is a bunch of modules and sub-packages organized by directory. _ init _. py in the directory
The file stores the package information.
· You can use import, import as, from import and other statements to import modules and packages.
The above is all about a brief understanding of the concept of the python module, and I hope to help you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!