Day 17 - How Modules Works
In programming language module means reusability without writing same thing again and again, how can you call a previously written block of code from different places of your application. The purpose of this excercise is to reduce the number of lines of code. If your code lines are more then it is difficult to maintain later. Plus if you need to change something you might end up changing in many places same thing. Assume that there is a spelling mistake and you need to correct it. You then need to correct in many different places.
Let’s see how modularity works in Python
. If you have the below code and save to a file hellomodule.py
, the you can use the name of this file as Python module.
#Funtion to be called
def sayHello(name):
print("Hello "+name)
In this case it will be hellomodule
. Notice here we are not using .py
extention in the module name.
How you can call the code,
import hellomodule
hellomodule.sayHello("Wriju")
In python you can also name a module and use shorter name, like below
import hellomodule as h
h.sayHello("Wriju")