<>import Function calls the entire module
example :
First build one python file
pizza.py
def make_pizza(size,*toppings): print('\nmaking a' + str(size)+'-inch pizza
with the following topping') for topping in toppings: print('-' + topping)
stay Another in the same directory python Call in pizza Functions in files
make_pizza.py
import pizza pizza.make_pizza(16,'pepperoni') pizza.make_pizza(12,'mushrooms',
'green peppers','extra cheese')
File at this time make_pizza.py Can access pizza.py Functions in files .
<> Import a specific function from another file
example :
First build one python file
pizza.py
def make_pizza(size,*toppings): print('\nmaking a' + str(size)+'-inch pizza
with the following topping') for topping in toppings: print('-' + topping)
Another in the same directory python Call in pizza Functions in files
make_pizza.py
from pizza import make_pizza make_pizza(16,'pepperoni') make_pizza(12,
'mushrooms','green peppers','extra cheese')
At this time, the file name of the call does not need to be written in the file , Just use the function directly .
<> Modify the name of the called function
To prevent the called function from conflicting with the function in the file , You can change the name of the called function .
example :
First build one python file
pizza.py
def make_pizza(size,*toppings): print('\nmaking a' + str(size)+'-inch pizza
with the following topping') for topping in toppings: print('-' + topping)
Another in the same directory python Call in pizza Functions in files
make_pizza.py
from pizza import make_pizza as xin_pizza xin_pizza(16,'pepperoni') xin_pizza(
12,'mushrooms','green peppers','extra cheese')
<> Modify the name of the called file
example :
First build one python file
pizza.py
def make_pizza(size,*toppings): print('\nmaking a' + str(size)+'-inch pizza
with the following topping') for topping in toppings: print('-' + topping)
Another in the same directory python Call in pizza Functions in files
make_pizza.py
import pizza as xin_pizza xin_pizza.make_pizza(16,'pepperoni') xin_pizza.
make_pizza(12,'mushrooms','green peppers','extra cheese')
<> Import all functions in the module
be careful , Try not to use when writing a large number of functions This method , Easy to override other functions .
example :
First build one python file
pizza.py
def make_pizza(size,*toppings): print('\nmaking a' + str(size)+'-inch pizza
with the following topping') for topping in toppings: print('-' + topping)
Another in the same directory python Call in pizza Functions in files
make_pizza.py
import pizza import* xin_pizza.make_pizza(16,'pepperoni') xin_pizza.make_pizza(
12,'mushrooms','green peppers','extra cheese')
Technology