A brief understanding of python module concepts and python module concepts

Source: Internet
Author: User

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!

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.