Python Getting Started Guide 3, module

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.