A module can contain variables, functions, classes or C, C++ extensions, etc.
Now I define a model.py module, which defines variables, functions, and classes
The file name is model.py, and the module noun is model
model module
Module usage: Take the above model module as an example, create a new module user_model.py
1、 import model: import module
# user_model.py file, the module name is called user_model
import model
print(model.member) #Call the variables in the model module
model.func() #Call the function in the model module
p = model.Person() #Instantiate the Person class in the model
p.fun() #Call methods in the Person class
2、 from model import Person: Import a single member
# user_model.py file, the module name is called user_model
from model import member
from model import func
from model import Person
print(member) #Return: Interpretation of global variables: call variables in the model module
func() #Return: a function interpretation: call the function in the model module
p =Person() #Interpretation: instantiate the Person class in the model
p.fun() #Return: A method interpretation: call a method in the Person class
3、 from model import *: import all members
# user_model.py file, the module name is called user_model
from model import*print(member) #Return: Interpretation of global variables: call variables in the model module
func() #Return: a function interpretation: call the function in the model module
p =Person() #Interpretation: instantiate the Person class in the model
p.fun() #Return: A method interpretation: call a method in the Person class
4、 from model import Person as P: Add an alias to Person
# user_model.py file, the module name is called user_model
from model import member as M
from model import func as F
from model import Person as P
print(M) #Return: Interpretation of global variables: call variables in the model module
F() #Return: a function interpretation: call the function in the model module
p =P() #Interpretation: instantiate the Person class in the model
p.fun() #Return: A method interpretation: call a method in the Person class
5、 Import multiple modules (modules are only imported once, and written multiple times are only imported once)
# Two ways, the second method is recommended
import sys, os #The first way
import sys #The second way
import os
pkg package and web package
Package usage: call members in the model module in the page module
1、 import pkg: When importing a package, only the init.py file in the package is executed
# pkg package__init__.py file
from pkg import model
# page.py file, module page
import pkg #If this__init__.The py file is empty, so the import cannot be used
print(pkg.model.member) #Interpretation: call variables in the model module
pkg.model.func() #Interpretation: call the function in the model module
p = pkg.model.Person() #Interpretation: instantiate the Person class in the model
p.fun() #Interpretation: call methods in the Person class
2、 from pkg import model: import module
# page.py file, module page
from pkg import model
print(model.member) #Interpretation: call variables in the model module
model.func() #Interpretation: call the function in the model module
p = model.Person() #Interpretation: instantiate the Person class in the model
p.fun() #Interpretation: call methods in the Person class
3、 from pkg.model import Person: Import members
# page.py file, module page
from pkg.model import member
from pkg.model import func
from pkg.model import Person
print(member) #Interpretation: call variables in the model module
func() #Interpretation: call the function in the model module
p =Person() #Interpretation: instantiate the Person class in the model
p.fun() #Interpretation: call methods in the Person class
4、 from ... import ... as ...; all imports can add as alias
1000 words omitted here
5、 Import multiple modules (modules are only imported once, and written multiple times are only imported once)
# Import module: two ways, the second method is recommended
from pkg import model, user_model #The first way
from pkg import model #The second way
from pkg import user_model
# Import members: two ways, the second method is recommended
form pkg.model import member, func, Person #The first way
from pkg.model import member #The second way
from pkg.model import func
from pkg.model import Person
Reprinted from today's headlines: python can understand at a glance
Recommended Posts