You are on page 1of 7

Modules:

--------
Def: Module is the collection of variables, functions, classes,....
Def: Module is a python file contains variables, functions, classes,....

In Python applications, Modules are providing the follolwing advantages.


1. Modularization
2. Abstraction
3. Security
4. Sharability
5. Reusability

In Python applications, there are two types of Modules.


1. Pre Defined Modules
2. User defined Modules

1. Pre Defined Modules:


------------------------
These modules are defined by Python programming language and which are provided
along with Python Software.
EX:
functool
math
numpy
random
tkinter
---
---

2. User defined Modules:


-------------------------
These modules are defined by the developers as per their application requirements.

To use modules in python applications we have to use the following two steps.
1. Create Module.
2. Access members from module

1. Create Module:
------------------
In Python applications, Every python file is treated as module,so to create module
we have to create a python file with .py extension.
EX: mymath.py
-------------
a = 10
b = 20
def add():
print(a+b)
def sub():
print(a-b)

2. Access Members from module


------------------------------
To access members from module we have to use "import" statement.
Syntax-1:
import moduleName
--> It able to import all the members from the specified module, where if we want
to use the members then we must use moduleName explicitly.
EX:
---
cal.py
-------
a = 10
b = 5
def add():
print("ADD :",(a+b))
def sub():
print("SUB :",(a-b))
def mul():
print("MUL :",(a*b))

test.py
-------
import cal
print("a :",cal.a)
print("b :",cal.b)
cal.add()
cal.sub()
cal.mul()
D:\python10>py test.py
a : 10
b : 5
ADD : 15
SUB : 5
MUL : 50

2. from moduleName import memberName


--> It able to import only the specified member from the specified modcule, where
to access the member it is not required to use module name.
EX:
---
cal.py
------
a = 10
b = 5
def add():
print("ADD :",(a+b))
def sub():
print("SUB :",(a-b))
def mul():
print("MUL :",(a*b))

test.py
---------
from cal import a
from cal import b
from cal import add
from cal import sub
from cal import mul

print("a :",a)
print("b :",b)
add()
sub()
mul()
D:\python10>py test.py
a : 10
b : 5
ADD : 15
SUB : 5
MUL : 50

--> In Python applications, it is possible to import more than one individual


member by using simple import statement.
Syntax: from moduleName import member1,member2,...member-n
EX:
---
cal.py
------
a = 10
b = 5
def add():
print("ADD :",(a+b))
def sub():
print("SUB :",(a-b))
def mul():
print("MUL :",(a*b))

test.py
--------
from cal import a,b,add,sub,mul

print("a :",a)
print("b :",b)
add()
sub()
mul()

D:\python10>py test.py
a : 10
b : 5
ADD : 15
SUB : 5
MUL : 50

Note: By using single import statement we are able to import all the members of the
specified module by using * notation also.
Syntax: from moduleName import *
EX:
---
cal.py
------
a = 10
b = 5
def add():
print("ADD :",(a+b))
def sub():
print("SUB :",(a-b))
def mul():
print("MUL :",(a*b))

test.py
--------
from cal import *

print("a :",a)
print("b :",b)
add()
sub()
mul()

D:\python10>py test.py
a : 10
b : 5
ADD : 15
SUB : 5
MUL : 50

--> In Python applications, it is possible to define alias names to the module


names by using 'as' , in this context, once if we provide alias names to the module
names then we are able to use alias names only to access the members , it is not
possible to use original module name.
EX:
---
cal.py
------
a = 10
b = 5
def add():
print("ADD :",(a+b))
def sub():
print("SUB :",(a-b))
def mul():
print("MUL :",(a*b))

test.py
---------
import cal as math
print("a :",math.a)
print("b :",math.b)
math.add()
math.sub()
math.mul()

D:\python10>py test.py
a : 10
b : 5
ADD : 15
SUB : 5
MUL : 50

--> In Python applications, we are able to provide alias names to the module
members also.
Syntax: from moduleName import memberName1 as aliasName1, memberName1 as
aliasName2,..
EX:
---
cal.py
------
a = 10
b = 5
def add():
print("ADD :",(a+b))
def sub():
print("SUB :",(a-b))
def mul():
print("MUL :",(a*b))

test.py
-------
from cal import a as x,b as y, add as sum, sub as diff, mul as prod
print("a :",x)
print("b :",y)
sum()
diff()
prod()

Q)Find the valid python import statements from the following list?
-------------------------------------------------------------------
1. from cal ---> Invalid
2. from cal * ---> Invalid
3. import cal ----> Valid
4. import cal * ----> Invalid
5. from cal import * ---> Valid
6. from cal import a ---> valid
7. from cal import a,b,add,sub,mul ----> valid

Q)What is the difference between the following two import statements?


----------------------------------------------------------------------
1. import cal
2. from cal import *

Ans:
----
To import members from cal module if we use "import cal" syntax then we must use
module name to access members.

To import members gtom cal module if we use "from cal import *" syntax then it is
not required to use module name to access members.

-->In Python applications, when we execute modules , PVM will compile the modules
and PVM will generate a seperate compiled file at the location "ApplicationFolder\
__pycache__\ fileName.cpython-37.pyc

EX:D:\python10\app05\__pycache__\cal.cpython-37.pyc

--> In Python applications, a particular module will be loaded only one time even
though we have provided multiple times import statements.
EX:
---
hello.py
--------
print("Hello, This is from hello module")

test.py
--------
import hello
import hello
import hello
import hello
import hello
print("Hello, This is from test module")
D:\python>py test.py
Hello, This is from hello module
Hello, This is from test module

--> As per the application requirement, if we want to perform modifications


frequently in a module and if we want to get all these modifications in our present
python application then we have to reload module time to time . To reload module we
have to use a predefined function reload(--) from "importlib" module.
EX:
---
hello.py
--------
msg = "Good Morning"

test.py
--------
from importlib import reload
import hello

print(hello.msg)
print(hello.msg)
print("Application is in Pausing state, perform modifications on hello module")
val = input()
reload(hello)
print(hello.msg)
print(hello.msg)
print(hello.msg)

EX:
---
TicketReservation.py
---------------------
no_Of_Vacancies = 3
customers_And_SeatNos = {}
def bookTocket(seatNo, customerName):
global no_Of_Vacancies
if no_Of_Vacancies > 0:
customers_And_SeatNos[seatNo] = customerName
no_Of_Vacancies = no_Of_Vacancies - 1
print("Hello",customerName, "Your Ticket is confirmed and your seat
no",seatNo)
else:
print("No Tickets are available")

def getStatus():
if no_Of_Vacancies != 0:
print("No of Vacancies :",no_Of_Vacancies)
print("Select Seat number except ",list(customers_And_SeatNos.keys()))
else:
print("All seats are Filled")

test.py
--------
from importlib import reload
import TicketReservation as res
while True:
res.getStatus()
seatNo = int(input("Enter Seat No : "))
customerName = input("Enter Customer Name :")
res.bookTocket(seatNo,customerName)
option = input("Onemore Ticket[yes/no]? :")
if option == "yes":
if(res.no_Of_Vacancies == 0):
break
else:
continue
else:
break

You might also like