Overloading@python
Overloading@python
Overloading@python
• Multiple Inner Classes can be created where a class contains more than one inner class.
Example 1:
class Outer:
"""Outer Class"""
def __init__(self):
## Instantiating the 'Inner' class
self.inner = self.Inner()
## Instantiating the '_Inner' class
self._inner = self._Inner()
def show_classes(self):
print("This is Outer class")
1 SS
PYTHON
print(inner)
print(_inner)
class Inner:
"""First Inner Class"""
def inner_display(self, msg):
print("This is Inner class")
print(msg)
class _Inner:
"""Second Inner Class"""
def inner_display(self, msg):
print("This is _Inner class")
print(msg)
## ...
Example 2:
class Outer:
def __init__(self):
self.__x = 20
self.__str = "Outer Class"
def show(self):
self.i1 = self.Inner()
self.i1.display()
def fn(self):
print("Hello from fn() of Outer Class")
print("str : ", self.__str)
class Inner:
def __init__(self):
self.__str_in = "Inner Class"
self.outer=Outer()
def display(self):
print("str_in : ", self.__str_in)
print("Inside display() of Inner Class")
print("x : ", self.outer._Outer__x)
print("str : ", self.outer._Outer__str)
self.outer.fn()
obj = Outer()
obj.show()
class Inner:
def __init__(self):
self.__str_in = "Inner Class"
self.outer=Outer()
def display(self):
print("str_in : ", self.__str_in)
print("Inside display() of Inner Class")
print("x : ", self.outer.x)
print("str : ", self.outer.str)
self.outer.fn()
obj = Outer()
obj.show()
Overloading
Overloading, in the context of programming, refers to the ability of a function or an operator to
behave in different ways depending on the parameters that are passed to the function, or the
operands that the operator acts on. The same built-in operator or function shows different behaviour
for objects of different classes.
1. Operator Overloading
Consider that we have two objects which are a physical representation of a class (user-defined data
type) and we have to add two objects with binary ‘+’ operator it throws an error, because compiler
don’t know how to add two objects. So we define a method for an operator and that process is called
operator overloading. This special method automatically invoked when it is associated with that
particular operator. Operator Overloading means giving extended meaning beyond their predefined
operational meaning. For example, when we use + operator, the magic method __add__ is
automatically invoked in which the operation for + operator is defined whether to add two integers or
join two strings and merge two lists. There by changing this magic method’s code, we can give extra
meaning to the + operator.
Example 1:
# Python Program illustrate how to overload an binary + operator
class A:
3 SS
PYTHON
Example 2:
# 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")
Output :
ob2 is greater than ob1
Example 3:
# Python program to overload equality and less than operators
class A:
def __init__(self, a):
self.a = a
def __lt__(self, other):
if(self.a<other.a):
return "ob1 is less than ob2"
else:
return "ob2 is less than ob1"
def __eq__(self, other):
if(self.a == other.a):
return "Both are equal"
else:
return "Not equal"
4 SS
PYTHON
ob1 = A(2)
ob2 = A(3)
print(ob1 < ob2)
ob3 = A(4)
ob4 = A(4)
print(ob1 == ob2)
Output :
ob1 is less than ob2
Not equal
Example:
int area(int length, int breadth) {
return length * breadth;
}
float area(int radius) {
return 3.14 * radius * radius;
}
To overload a user-defined function in Python, we need to write the function logic in such a way that
depending upon the parameters passed, a different piece of code executes inside the function. Example:
class Student:
def hello(self, name=None):
if name is not None:
print('Hi ' + name)
else:
print('Hi ')
# Creating a class instance
std = Student()
# Call the method
std.hello()
# Call the method and pass a parameter
std.hello('Indian')
Output:
Hi
Hi Indian
The class Student has one function named hello(). The class takes the parameter name which has been
set to None. This means the method can be called with or without a parameter. We have created an
instance of the class which has been used to call the function twice, first with zero parameters and
secondly with one parameter.
6 SS