Module (modules)
To meet the convenience of the maintenance program, Python provides a way to get the definition from a file, and the module refers to a file that includes Python definitions and declarations, whose file name is the module name +.py suffix, and the module name of the module can be __name__ by the global variable.
The file structure is created as follows, where modu.py is the called module, Transfer,py calls the module:
modu.py content is as follows:
Write transfer.py now
#导入模块, introduced only the module name Modu
from getstartguide.modu import modu
#想要访问模块下的函数, you need to use the following method
print ( dir (modu)) #返回字符串类型的存储列表
print (Modu. __name__)
Operation Result:
Modu.module1 ()
Modu.module2 ()
Operation Result:
#还可为其赋一个本地名称
Function1=modu.module1
Function1 ()
Operation Result:
< Span style= "COLOR: #000080" > # You can also directly introduce the function < Span style= "COLOR: #000080" >
Span style= "COLOR: #000080" >from GetStartGuide.Modu.modu import Module1
from GetStartGuide.Modu.modu import module2
#直接调用即可
Module1 ()
Module2 ()
< Span style= "COLOR: #000080" > < Span style= "COLOR: #000080" > run Result:
Python Getting Started Guide 3, module