We can also import something specific (variable or function or class) from a module instead of importing the whole module. This is achieved using the from statement, such as the following:
from math import pi
Another best practice is to use a different name for an imported module for convenience or sometimes when the same names are being used for different resources in two different libraries. To illustrate this idea, we will be updating our calcmain1.py file (the updated program is calcmain2.py) from the earlier example by using the calc and rand aliases for the mycalculator and myrandom modules, respectively. This change will
make use of the modules in the main script much simpler, as shown next:
# calcmain2.py with alias for modules
import mycalculator as calc
import myrandom as rand
def my_main():
""" This is a main function which generates two random\
numbers and then apply calculator functions on them """
x = rand.random_2d()
y = rand.random_1d()
sum = calc.add(x,y)
diff = calc.subtract(x,y)
print("x = {}, y = {}".format(x,y))
print("sum is {}".format(sum))
print("diff is {}".format(diff))
""" This is executed only if the special variable '__name__' is
set as main"""
if __name__ == "__main__":
my_main()
As a next step, we will combine the two concepts discussed earlier in the next iteration of the calcmain1.py program (the updated program is calcmain3.py). In this update, we will use the from statement with the module names and then import the individual functions from each module. In the case of the add and subtract functions, we used the as statement to define a different local definition of the module resource for illustration purposes.
A code snippet of calcmain3.py is as follows:
# calcmain3.py with from and alias combined
from mycalculator import add as my_add
from mycalculator import subtract as my_subtract
from myrandom import random_2d, random_1d
def my_main():
""" This is a main function which generates two random
numbers and then apply calculator functions on them """
x = random_2d()
y = random_1d()
sum = my_add(x,y)
diff = my_subtract(x,y)
print("x = {}, y = {}".format(x,y))
print("sum is {}".format(sum))
print("diff is {}".format(diff))
print (globals())
""" This is executed only if the special variable '__name__' is
set as main"""
if __name__ == "__main__":
my_main()
As we used the print (globals()) statement with this program, the console output of this program will show that the variables corresponding to each function are created as per our alias. The sample console output is as follows:
{
"__name__":"__main__",
"__doc__":"None",
"__package__":"None",
"__loader__":"<_frozen_importlib_external.\
SourceFileLoader object at 0x1095f1208>",
"__spec__":"None",
"__annotations__":{},
"__builtins__":"<module 'builtins' (built-in)>", "__
file__":"/PythonForGeeks/source_code/chapter2/module1/
main_2.py",
"__cached__":"None",
"my_add":"<function add at 0x109645400>",
"my_subtract":"<function subtract at 0x109645598>",
"random_2d":"<function random_2d at 0x10967a840>",
"random_1d":"<function random_1d at 0x1096456a8>",
"my_main":"<function my_main at 0x109645378>"
}
Note that the variables in bold correspond to the changes we made in the import statements in the calcmain3.py file.
0 comments:
Post a Comment