Modules and Packages,Polymorphism,Abstract Class
Modules and Packages,Polymorphism,Abstract Class
Python Modules
•A python module can be defined as a python program
file which contains a python code including python
functions, class, or variables.
•In other words, we can say that our python code file
saved with the extension (.py) is treated as the module.
We may have a runnable code inside the python module.
def displayMsg(name)
• print("Hi "+name);
Loading the module in our python
code
The import statement
The from-import statement
import Employees
print(Employees.getNames())
Polymorphism
• The word polymorphism means having many forms.
• In programming, polymorphism means the same function name (but different signatures) being used for
different types.
• The key difference is the data types and number of arguments used in function.
1.Function Overloading
Two or more methods have the same name but different numbers of parameters or
different types of parameters, or both. These methods are called overloaded
methods, and this is called method overloading.
# Driver code
print(add(2, 3))
print(add(2, 3, 4))
class India():
def capital(self):
print("New Delhi is the capital of India.")
def language(self):
print("Hindi is the most widely spoken language of India.")
def type(self):
print("India is a developing country.")
class USA():
def capital(self):
print("Washington, D.C. is the capital of USA.")
def language(self):
print("English is the primary language of USA.")
def type(self):
print("USA is a developed country.")
obj_ind = India()
obj_usa = USA()
for country in (obj_ind, obj_usa):
country.capital()
country.language()
country.type()
polymorphism in Python using inheritance
and method overriding
class Animal:
def speak(self):
raise NotImplementedError("Subclass must implement this method")
class Dog(Animal):
def speak(self):
return "Woof!"
class Cat(Animal):
def speak(self):
return "Meow!“
# Create a list of Animal objects
animals = [Dog(), Cat()]
# Call the speak method on each object
for animal in animals:
print(animal.speak())
Output
Woof!
Meow!
Operator Overloading
• It means giving extended meaning beyond their
predefined operational meaning.
• For example, operator + is used to add two integers as
well as join two strings and merge two lists.
• It is achievable because ‘+’ operator is overloaded by
int class and str class.
# Python program to show use of + operator for different purposes.
print(1 + 2)
class complex:
def __init__(self, a, b):
self.a = a
self.b = b
Ob1 = complex(1, 2)
Ob2 = complex(2, 3)
Ob3 = Ob1 + Ob2
print(Ob3)
# Python program to overload a comparison operators
class A:
def __init__(self, a):
self.a = a
def __gt__(self, other):
if(self.a>other.a):
return True
else:
return False
ob1 = A(2)
ob2 = A(3)
if(ob1>ob2):
print("ob1 is greater than ob2")
else:
print("ob2 is greater than ob1")
Binary Operators
Operator Magic Method
+ __add__(self, other)
– __sub__(self, other)
* __mul__(self, other)
/ __truediv__(self, other)
// __floordiv__(self, other)
% __mod__(self, other)
** __pow__(self, other)
>> __rshift__(self, other)
<< __lshift__(self, other)
& __and__(self, other)
| __or__(self, other)
^ __xor__(self, other)
Comparison Operator