Table of Contents
User defined modules
Before we explain user defined modules let’s have glims on ‘what are modules?’.
Python has extensive libraries for supporting various functionalities like graphical user interface, database connectivity, mathematical calculations etc.
A file which consists of a collection of related functions is called as a module.
Python has many built-in modules like math, time, random etc.
Python also allows user to define its own modules according to the requirement such modules are called as user defined modules.
To use or call these modules in our script we use specific keywords like import and from.
In this post we will be studying math, time and random module in detail along with user defined modules.
What are user defined modules?
Python allows us to create and import our own module with desired functionalities.
To create a module of our choice we must first decide the functionalities required and choice the name of the module accordingly.
To create a module first step after deciding the module name is to create a file in python and save it with an extension of “.py”.
Keyword should not be use as a module name.
Whenever you create a module in it can be called in any other file using “import” keyword.
It is important run “module_name.py” i.e. file or module created by us before calling it in another script.
If we call our user defined module in any file without executing the module or file it will generate and import error.
Advantages of user defined modules
- With the help of user defined modules we can enable the use of new functionalities defined by user.
- It helps in better code management and reusability.
- Only required functionalities are included while creating user defined modules which saves line of code and improves performance.
The dir function
- The dir() function is python built in function which is used to display the list of functions defined in a module.
- It returns the list datatype consisting strings of function names separated by comma.
- All attributes beginning with an underscore are default python attributes associated with a module.
- dir() can return both built-in functions as well as user defined functions present in module.
Syntax of dir()
dir(module)
Example of user defined modules - 1
geometry.py
import math def sphereArea(r): return 4*math.pi*r**2 def sphereVolume(r): return 4*math.pi*r**3/3 def sphereMetrics(r): return SphereArea(r).sphereVolume(r) def circleArea(r): return math.pi*r**2 def squareArea(x): return x**2
demo.py
import geometry
def pointyShapeVolume(x,h,square):
if square:
base=geometry.squareArea(x)
else:
base=geometry.circleArea(x)
return h*base/3.0
print("Output of user defined function in python")
print("--------------------------------------------------------------------------")
print(dir(geometry))
print(pointyShapeVolume(9,3.6,True))
print(pointyShapeVolume(12,2.4,False))
Output

Example of user defined modules - 2
task.py
task = []
completed = []
def add(i):
task.append(i)
def delete(i):
print("n"+i + " is deleted from the list")
task.remove(i)
def view():
n = 1
print("nTasks in list")
for t in task:
print(str(n)+" : "+t)
n = n + 1
n = 1
if(completed != []):
print("Tasks Completed")
for t in completed:
print(str(n)+" : "+t)
n = n + 1
else:
print("No task completed yetn")
def done(i):
print("n"+ i + " is donen")
task.remove(i)
completed.append(i)
to-do-list.py
import task as todolist
todolist.add("Milk")
todolist.add("Egg")
todolist.add("Bread")
todolist.view()
todolist.done("Bread")
todolist.view()
todolist.delete("Egg")
todolist.view()
Output
Tasks in list
1 : Milk
2 : Egg
3 : Bread
No task completed yet
Bread is done
Tasks in list
1 : Milk
2 : Egg
Tasks Completed
1 : Bread
Egg is deleted from the list
Tasks in list
1 : Milk
Tasks Completed
1 : Bread
Other python related post
Other advance python related post
