Store the function in the module (2)

Store the function in the module (2)

Import specific functions:

from pizza import make_pizza 
make_pizza(16, 'pepperoni') 
make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')
 

Only the make_pizza function in pizza.py is imported here. If this syntax is used, there is no need to use a period when calling the function.

Import all functions:

from pizza import * 
make_pizza(16, 'pepperoni') 
make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')
 

The asterisk in the import statement tells Python to copy every function in the module pizza to this program file. Since each function is imported, each function can be called by name without using period notation. However, when using a large module that you did not write by yourself, it is best not to use this import method: If the name of a function in the module is the same as the name used in your project, it may lead to unexpected results: Python may encounter multiple Functions or variables with the same name will overwrite the functions instead of importing all functions separately. The best practice is to either import only the functions you need to use, or import the entire module and use period notation. This makes the code clearer and easier to read and understand.

Use as to alias a function:

from pizza import make_pizza as mp 
mp(16, 'pepperoni') 
mp(12, 'mushrooms', 'green peppers', 'extra cheese'
 

This makes programming easier

Use as to name the module

import pizza as p 
p.make_pizza(16, 'pepperoni') 
p.make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')
 

This will make the code concise

BY: Python programming: from entry to practice.