Take a brief look at python module concepts

  • 2020-06-23 01:02:39
  • OfStack

This article focuses on the concepts of modules in Python, as follows.

Modules are the basic way python organizes code:

python's scripts are saved with a text file with the extension py.
1 script can be run separately or imported into another script to run.
When a script is imported to run in another script, we call it a module.

The module name is the same as the script file name:

Let's say you have an ES16en.py script,
You can import it in another script using the import items statement

This is the python code named ES24en.py, which will be imported as the cal module in the following code:


#!/usr/bin/python
#coding:utf-8

from __future__ import division

def jia(x,y):
  return x+y

def jian(x,y):
  return x-y

def cheng(x,y):
  return x*y

def chu(x,y):
  return x/y

def 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:
    pass
if __name__=="__main__":
  operator(2,'+',4)

There are three ways to actually import the cal module


#import cal

#print cal.jia(1,2)

#import cal as c

#print c.jia(1,2)

from cal import jia

print jia(1,2)

Another one is the package import module, which is commonly used in that there are many modules to be managed under the same package:

Create 1 per ___ with the module code under the package (folder name test) with a double underscore with each ___ (es38EN__).


import test.cal
cal.jia(1,2)

Conclusion:

· Module is an python script file that can be imported;

· The package is a heap of modules and subpackages organized by directory, with each ___ 49EN__.py
The file holds the package information

· Modules and packages can be imported using import, import as, from import and other statements

Above on this article on a simple understanding of the python module concept, I hope to help you. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!


Related articles: