Modules (module)
In Python, a. py file is a module. Through the module, you can invoke programs in other files
Introducing Modules
Write a first.py file that reads as follows:
def Letter (): print(' ABC ')
Write a second.py file that reads as follows:
Import firstfor i in Range ( ): First.letter ()
In the second.py file, we used the letter () function defined in first.py.
Once the module is introduced, we can invoke an object in the module by means of the module. Object
How to introduce modules in Python
Import a as B # introduces module a renamed to B
From a import function1 # A Function1 object is introduced from module A, and when you call an object in a, we don't use the a.function1 directly with Function1.
From a import * #调用时直接使用对象 instead of a. Object
The following three formulations are the same
Notation 1
import first as Afor i in range: a.letter ()
Notation 2
Import letterfor i in range (+): Letter ()
Notation 3
Import *for i in range (+): Letter ()
Module Package
We can put files of similar function in a folder (for example: Exp_dir), constitute a module package, through
import exp_dir.module
Introduce module modules in the This_dir folder.
The folder must contain a __init__.py file that reminds Python that the folder is a module package. __init__.py can be an empty file.
"Python" Learning Note 12: Modules