Skip to content

Method Overloading in Python

Compile-time Polymorphism

Compile-time polymorphism is also known as static polymorphism. It occurs when the compiler knows which polymorphic function to call at compile-time. Compile-time polymorphism is achieved through function overloading and operator overloading.

Function Overloading

Function overloading is a feature of a programming language that allows a function to be defined more than once. The function is defined with the same name but with different parameters. The function is called based on the number of parameters passed to it. Function overloading is a type of compile-time polymorphism. It is achieved by defining multiple functions with the same name but with different parameters. The function is called based on the number of parameters passed to it. Function overloading is not supported in Python. However, it can be achieved by using default arguments and variable-length arguments. Let’s see how to achieve function overloading in Python.

function_overloading.py
def add(a, b):
    print("Two arguments\n")
    return a + b
 
def add(a, b, c):
    print("Three arguments\n")
    return a + b + c
 
def add(a, b, c, d):
    print("Four arguments\n")
    return a + b + c + d
 
print(add(1, 2))
print(add(1, 2, 3))
print(add(1, 2, 3, 4))
function_overloading.py
def add(a, b):
    print("Two arguments\n")
    return a + b
 
def add(a, b, c):
    print("Three arguments\n")
    return a + b + c
 
def add(a, b, c, d):
    print("Four arguments\n")
    return a + b + c + d
 
print(add(1, 2))
print(add(1, 2, 3))
print(add(1, 2, 3, 4))

Output:

command
C:\Users\username>python function_overloading.py
Traceback (most recent call last):
  File "function_overloading.py", line 11, in <module>
    print(add(1, 2))
TypeError: add() missing 2 required positional arguments: 'c' and 'd'
command
C:\Users\username>python function_overloading.py
Traceback (most recent call last):
  File "function_overloading.py", line 11, in <module>
    print(add(1, 2))
TypeError: add() missing 2 required positional arguments: 'c' and 'd'

In the above example, we have defined three functions with the same name but with different parameters. The first function takes two arguments, the second function takes three arguments, and the third function takes four arguments. We have called the add()add() function with two, three, and four arguments. The output shows that the add()add() function is called based on the number of arguments passed to it. The add()add() function is called with two arguments, three arguments, and four arguments. In Python, function overloading is achieved by using default arguments and variable-length arguments. Let’s see how to achieve function overloading in Python using default arguments and variable-length arguments.

Function Overloading using Default Arguments

function_overloading.py
def add(a, b, c=0, d=0):
    # Default arguments
    return a + b + c + d
 
print(add(1, 2))
print(add(1, 2, 3))
print(add(1, 2, 3, 4))
function_overloading.py
def add(a, b, c=0, d=0):
    # Default arguments
    return a + b + c + d
 
print(add(1, 2))
print(add(1, 2, 3))
print(add(1, 2, 3, 4))

Output:

command
C:\Users\username>python function_overloading.py
3
6
10
command
C:\Users\username>python function_overloading.py
3
6
10

In the above example, we have defined the add()add() function with four parameters. The cc and dd parameters have default values of 00. We have called the add()add() function with two, three, and four arguments. The output shows that the add()add() function is called based on the number of arguments passed to it. The add()add() function is called with two arguments, three arguments, and four arguments.

Function Overloading using Variable-length Arguments

function_overloading.py
def add(*args):
    # Variable-length arguments
    total = 0
    for arg in args:
        total += arg
    return total
 
print(add(1, 2))
print(add(1, 2, 3))
print(add(1, 2, 3, 4))
function_overloading.py
def add(*args):
    # Variable-length arguments
    total = 0
    for arg in args:
        total += arg
    return total
 
print(add(1, 2))
print(add(1, 2, 3))
print(add(1, 2, 3, 4))

Output:

command
C:\Users\username>python function_overloading.py
3
6
10
command
C:\Users\username>python function_overloading.py
3
6
10

In the above example, we have defined the add()add() function with a variable-length argument. We have called the add()add() function with two, three, and four arguments. The output shows that the add()add() function is called based on the number of arguments passed to it. The add()add() function is called with two arguments, three arguments, and four arguments.

Function Overloading in class

Function overloading is a feature of a programming language that allows a function to be defined more than once. The function is defined with the same name but with different parameters. The function is called based on the number of parameters passed to it. Function overloading is a type of compile-time polymorphism. It is achieved by defining multiple functions with the same name but with different parameters. The function is called based on the number of parameters passed to it. Function overloading is not supported in Python. However, it can be achieved by using default arguments and variable-length arguments. Let’s see how to achieve function overloading in Python.

function_overloading.py
class Student:
    def __init__(self, name, roll):
        self.name = name
        self.roll = roll
 
    def display(self):
        print('Name:', self.name)
        print('Roll:', self.roll)
 
    def display(self, age):
        print('Name:', self.name)
        print('Roll:', self.roll)
        print('Age:', age)
 
student1 = Student('John', 1)
student1.display()
student1.display(20)
function_overloading.py
class Student:
    def __init__(self, name, roll):
        self.name = name
        self.roll = roll
 
    def display(self):
        print('Name:', self.name)
        print('Roll:', self.roll)
 
    def display(self, age):
        print('Name:', self.name)
        print('Roll:', self.roll)
        print('Age:', age)
 
student1 = Student('John', 1)
student1.display()
student1.display(20)

Output:

command
C:\Users\username>python function_overloading.py
Traceback (most recent call last):
  File "function_overloading.py", line 17, in <module>
    student1.display()
TypeError: display() missing 1 required positional argument: 'age'
command
C:\Users\username>python function_overloading.py
Traceback (most recent call last):
  File "function_overloading.py", line 17, in <module>
    student1.display()
TypeError: display() missing 1 required positional argument: 'age'

In the above example, we have defined two display()display() methods in the StudentStudent class. The first display()display() method takes one argument, and the second display()display() method takes two arguments. We have created an object named student1student1 of the StudentStudent class. We have called the display()display() method of the student1student1 object with one argument. The output shows that the display()display() method is called with one argument. We have called the display()display() method of the student1student1 object with two arguments. The output shows that the display()display() method is called with two arguments. In Python, function overloading is achieved by using default arguments and variable-length arguments. Let’s see how to achieve function overloading in Python using default arguments and variable-length arguments.

Operator Overloading

Operator overloading is a feature of a programming language that allows an operator to be defined more than once. The operator is defined with the same name but with different parameters. The operator is called based on the number of parameters passed to it. Operator overloading is a type of compile-time polymorphism. It is achieved by defining multiple operators with the same name but with different parameters. The operator is called based on the number of parameters passed to it. Operator overloading is not supported in Python. However, it can be achieved by using magic methods. Let’s see how to achieve operator overloading in Python.

operator_overloading.py
class Complex:
    def __init__(self, real, imag):
        self.real = real
        self.imag = imag
 
    def __add__(self, other):
        return Complex(self.real + other.real, self.imag + other.imag)
 
    def __sub__(self, other):
        return Complex(self.real - other.real, self.imag - other.imag)
 
    def __mul__(self, other):
        return Complex(self.real * other.real, self.imag * other.imag)
 
    def __truediv__(self, other):
        return Complex(self.real / other.real, self.imag / other.imag)
 
    def __str__(self):
        return f'{self.real} + {self.imag}j'
 
c1 = Complex(1, 2)
c2 = Complex(3, 4)
print(c1 + c2)
print(c1 - c2)
print(c1 * c2)
print(c1 / c2)
operator_overloading.py
class Complex:
    def __init__(self, real, imag):
        self.real = real
        self.imag = imag
 
    def __add__(self, other):
        return Complex(self.real + other.real, self.imag + other.imag)
 
    def __sub__(self, other):
        return Complex(self.real - other.real, self.imag - other.imag)
 
    def __mul__(self, other):
        return Complex(self.real * other.real, self.imag * other.imag)
 
    def __truediv__(self, other):
        return Complex(self.real / other.real, self.imag / other.imag)
 
    def __str__(self):
        return f'{self.real} + {self.imag}j'
 
c1 = Complex(1, 2)
c2 = Complex(3, 4)
print(c1 + c2)
print(c1 - c2)
print(c1 * c2)
print(c1 / c2)

Output:

command
C:\Users\username>python operator_overloading.py
4 + 6j
-2 + -2j
3 + 8j
0.3333333333333333 + 0.5j
command
C:\Users\username>python operator_overloading.py
4 + 6j
-2 + -2j
3 + 8j
0.3333333333333333 + 0.5j

In the above example, we have defined four magic methods named __add__()__add__(), __sub__()__sub__(), __mul__()__mul__(), and __truediv__()__truediv__(). The __add__()__add__() method is called when the ++ operator is used with two ComplexComplex objects. The __sub__()__sub__() method is called when the -- operator is used with two ComplexComplex objects. The __mul__()__mul__() method is called when the ** operator is used with two ComplexComplex objects. The __truediv__()__truediv__() method is called when the // operator is used with two ComplexComplex objects. We have created two ComplexComplex objects named c1c1 and c2c2. We have added, subtracted, multiplied, and divided the c1c1 and c2c2 objects. The output shows that the __add__()__add__(), __sub__()__sub__(), __mul__()__mul__(), and __truediv__()__truediv__() methods are called when the ++, --, **, and // operators are used with two ComplexComplex objects.

Magic Methods

Magic methods are special methods that are used to perform some special operations. They are also known as dunder methods. They are always surrounded by double underscores. They are called automatically when certain operations are performed on objects. For example, the __init__()__init__() method is called automatically when an object is created. Magic methods are also known as dunder methods because they are surrounded by double underscores. Let’s see how to use magic methods in Python.

List of Magic Methods

S.No.Magic MethodDescriptionExample
1__init__()__init__()Called when an object is created.__init__(self, name, roll)__init__(self, name, roll)
2__str__()__str__()Called when the str()str() function is called on an object.__str__(self)__str__(self)
3__repr__()__repr__()Called when the repr()repr() function is called on an object.__repr__(self)__repr__(self)
4__len__()__len__()Called when the len()len() function is called on an object.__len__(self)__len__(self)
5__add__()__add__()Called when the ++ operator is used with two objects.__add__(self, other)__add__(self, other)
6__sub__()__sub__()Called when the -- operator is used with two objects.__sub__(self, other)__sub__(self, other)
7__mul__()__mul__()Called when the ** operator is used with two objects.__mul__(self, other)__mul__(self, other)
8__truediv__()__truediv__()Called when the // operator is used with two objects.__truediv__(self, other)__truediv__(self, other)
9__floordiv__()__floordiv__()Called when the //// operator is used with two objects.__floordiv__(self, other)__floordiv__(self, other)
10__mod__()__mod__()Called when the %% operator is used with two objects.__mod__(self, other)__mod__(self, other)
11__pow__()__pow__()Called when the **** operator is used with two objects.__pow__(self, other)__pow__(self, other)
12__and__()__and__()Called when the && operator is used with two objects.__and__(self, other)__and__(self, other)
13__or__()__or__()Called when the `` operator is used with two objects.
14__xor__()__xor__()Called when the ^^ operator is used with two objects.__xor__(self, other)__xor__(self, other)
15__lt__()__lt__()Called when the << operator is used with two objects.__lt__(self, other)__lt__(self, other)
16__le__()__le__()Called when the <=<= operator is used with two objects.__le__(self, other)__le__(self, other)
17__eq__()__eq__()Called when the ==== operator is used with two objects.__eq__(self, other)__eq__(self, other)
18__ne__()__ne__()Called when the !=!= operator is used with two objects.__ne__(self, other)__ne__(self, other)
19__gt__()__gt__()Called when the >> operator is used with two objects.__gt__(self, other)__gt__(self, other)
20__ge__()__ge__()Called when the >=>= operator is used with two objects.__ge__(self, other)__ge__(self, other)
21__getitem__()__getitem__()Called when an item is accessed using the [][] operator.__getitem__(self, key)__getitem__(self, key)
22__setitem__()__setitem__()Called when an item is assigned using the [][] operator.__setitem__(self, key, value)__setitem__(self, key, value)
23__delitem__()__delitem__()Called when an item is deleted using the [][] operator.__delitem__(self, key)__delitem__(self, key)
24__contains__()__contains__()Called when the inin operator is used with an object.__contains__(self, item)__contains__(self, item)
25__call__()__call__()Called when an object is called as a function.__call__(self, *args, **kwargs)__call__(self, *args, **kwargs)
26__enter__()__enter__()Called when the withwith statement is used with an object.__enter__(self)__enter__(self)
27__exit__()__exit__()Called when the withwith statement is used with an object.__exit__(self, exc_type, exc_value, traceback)__exit__(self, exc_type, exc_value, traceback)
28__iter__()__iter__()Called when an object is iterated.__iter__(self)__iter__(self)
29__next__()__next__()Called when the next()next() function is called on an object.__next__(self)__next__(self)
30__reversed__()__reversed__()Called when the reversed()reversed() function is called on an object.__reversed__(self)__reversed__(self)
31__hash__()__hash__()Called when the hash()hash() function is called on an object.__hash__(self)__hash__(self)
32__bool__()__bool__()Called when the bool()bool() function is called on an object.__bool__(self)__bool__(self)
33__format__()__format__()Called when the format()format() function is called on an object.__format__(self, format_spec)__format__(self, format_spec)
34__index__()__index__()Called when an object is used as an index.__index__(self)__index__(self)
35__int__()__int__()Called when the int()int() function is called on an object.__int__(self)__int__(self)
36__float__()__float__()Called when the float()float() function is called on an object.__float__(self)__float__(self)
37__complex__()__complex__()Called when the complex()complex() function is called on an object.__complex__(self)__complex__(self)
38__round__()__round__()Called when the round()round() function is called on an object.__round__(self, n)__round__(self, n)
39__floor__()__floor__()Called when the math.floor()math.floor() function is called on an object.__floor__(self)__floor__(self)
40__ceil__()__ceil__()Called when the math.ceil()math.ceil() function is called on an object.__ceil__(self)__ceil__(self)
41__trunc__()__trunc__()Called when the math.trunc()math.trunc() function is called on an object.__trunc__(self)__trunc__(self)
42__pos__()__pos__()Called when the ++ operator is used with an object.__pos__(self)__pos__(self)
43__neg__()__neg__()Called when the -- operator is used with an object.__neg__(self)__neg__(self)
44__abs__()__abs__()Called when the abs()abs() function is called on an object.__abs__(self)__abs__(self)
45__invert__()__invert__()Called when the ~~ operator is used with an object.__invert__(self)__invert__(self)

init() Method

The __init__()__init__() method is called automatically when an object is created. It is used to initialize the instance variables of an object. It is also known as the constructor method. Let’s see how to use the __init__()__init__() method in Python.

init.py
class Student:
    def __init__(self, name, roll):
        self.name = name
        self.roll = roll
 
student1 = Student('John', 1)
print('Name:', student1.name)
print('Roll:', student1.roll)
init.py
class Student:
    def __init__(self, name, roll):
        self.name = name
        self.roll = roll
 
student1 = Student('John', 1)
print('Name:', student1.name)
print('Roll:', student1.roll)

Output:

command
C:\Users\username>python init.py
Name: John
Roll: 1
command
C:\Users\username>python init.py
Name: John
Roll: 1

In the above example, we have created two instance variables named namename and rollroll. We have initialized the namename and rollroll variables to the namename and rollroll parameters of the __init__()__init__() method. We have printed the namename and rollroll variables using the student1student1 object. The output shows that the namename and rollroll variables are unique to the object.

str() Method

The __str__()__str__() method is called when the str()str() function is called on an object. It is used to return a string representation of an object. Let’s see how to use the __str__()__str__() method in Python. It also called when the print()print() function is called on an object.

str.py
class Student:
    def __init__(self, name, roll):
        self.name = name
        self.roll = roll
 
    def __str__(self):
        return f'Name: {self.name}\nRoll: {self.roll}'
 
student1 = Student('John', 1)
print(str(student1))
print(student1)
str.py
class Student:
    def __init__(self, name, roll):
        self.name = name
        self.roll = roll
 
    def __str__(self):
        return f'Name: {self.name}\nRoll: {self.roll}'
 
student1 = Student('John', 1)
print(str(student1))
print(student1)

Output:

command
C:\Users\username>python str.py
Name: John
Roll: 1
Name: John
Roll: 1
command
C:\Users\username>python str.py
Name: John
Roll: 1
Name: John
Roll: 1

In the above example, we have defined the __str__()__str__() method in the StudentStudent class. The __str__()__str__() method returns a string representation of the StudentStudent object. We have created an object named student1student1 of the StudentStudent class. We have printed the student1student1 object using the str()str() function. The output shows that the __str__()__str__() method is called when the str()str() function is called on the student1student1 object.

repr() Method

The __repr__()__repr__() method is called when the repr()repr() function is called on an object. It is used to return a string representation of an object. Let’s see how to use the __repr__()__repr__() method in Python. It also called when the print()print() function is called on an object.

repr.py
class Student:
    def __init__(self, name, roll):
        self.name = name
        self.roll = roll
 
    def __repr__(self):
        return f'Name: {self.name}\nRoll: {self.roll}'
 
student1 = Student('John', 1)
print(student1)
repr.py
class Student:
    def __init__(self, name, roll):
        self.name = name
        self.roll = roll
 
    def __repr__(self):
        return f'Name: {self.name}\nRoll: {self.roll}'
 
student1 = Student('John', 1)
print(student1)

Output:

command
C:\Users\username>python repr.py
Name: John
Roll: 1
command
C:\Users\username>python repr.py
Name: John
Roll: 1

In the above example, we have defined the __repr__()__repr__() method in the StudentStudent class. The __repr__()__repr__() method returns a string representation of the StudentStudent object. We have created an object named student1student1 of the StudentStudent class. We have printed the student1student1 object using the print()print() function. The output shows that the __repr__()__repr__() method is called when the print()print() function is called on the student1student1 object.

len() Method

The __len__()__len__() method is called when the len()len() function is called on an object. It is used to return the length of an object. Let’s see how to use the __len__()__len__() method in Python.

len.py
class Store:
    def __init__(self, items):
        self.items = items
 
    def __len__(self):
        return len(self.items)
 
store1 = Store(['Apple', 'Banana', 'Orange'])
print(len(store1))
len.py
class Store:
    def __init__(self, items):
        self.items = items
 
    def __len__(self):
        return len(self.items)
 
store1 = Store(['Apple', 'Banana', 'Orange'])
print(len(store1))

Output:

command
C:\Users\username>python len.py
3
command
C:\Users\username>python len.py
3

In the above example, we have defined the __len__()__len__() method in the StoreStore class. The __len__()__len__() method returns the length of the itemsitems list. We have created an object named store1store1 of the StoreStore class. We have printed the store1store1 object using the len()len() function. The output shows that the __len__()__len__() method is called when the len()len() function is called on the store1store1 object.

add() Method

The __add__()__add__() method is called when the ++ operator is used with two objects. It is used to add two objects. Let’s see how to use the __add__()__add__() method in Python.

add.py
class Complex:
    def __init__(self, real, imag):
        self.real = real
        self.imag = imag
 
    def __add__(self, other):
        return Complex(self.real + other.real, self.imag + other.imag)
 
c1 = Complex(1, 2)
c2 = Complex(3, 4)
c3 = c1 + c2
print(f'{c1.real} + {c1.imag}j + {c2.real} + {c2.imag}j = {c3.real} + {c3.imag}j')
add.py
class Complex:
    def __init__(self, real, imag):
        self.real = real
        self.imag = imag
 
    def __add__(self, other):
        return Complex(self.real + other.real, self.imag + other.imag)
 
c1 = Complex(1, 2)
c2 = Complex(3, 4)
c3 = c1 + c2
print(f'{c1.real} + {c1.imag}j + {c2.real} + {c2.imag}j = {c3.real} + {c3.imag}j')

Output:

command
C:\Users\username>python add.py
1 + 2j + 3 + 4j = 4 + 6j
command
C:\Users\username>python add.py
1 + 2j + 3 + 4j = 4 + 6j

In the above example, we have defined the __add__()__add__() method in the ComplexComplex class. The __add__()__add__() method returns a ComplexComplex object. We have created two ComplexComplex objects named c1c1 and c2c2. We have added the c1c1 and c2c2 objects using the ++ operator. The output shows that the __add__()__add__() method is called when the ++ operator is used with two ComplexComplex objects.

sub() Method

The __sub__()__sub__() method is called when the -- operator is used with two objects. It is used to subtract two objects. Let’s see how to use the __sub__()__sub__() method in Python.

sub.py
class Complex:
    def __init__(self, real, imag):
        self.real = real
        self.imag = imag
 
    def __sub__(self, other):
        return Complex(self.real - other.real, self.imag - other.imag)
 
c1 = Complex(1, 2)
c2 = Complex(3, 4)
c3 = c1 - c2
print(f'{c1.real} + {c1.imag}j - {c2.real} + {c2.imag}j = {c3.real} + {c3.imag}j')
sub.py
class Complex:
    def __init__(self, real, imag):
        self.real = real
        self.imag = imag
 
    def __sub__(self, other):
        return Complex(self.real - other.real, self.imag - other.imag)
 
c1 = Complex(1, 2)
c2 = Complex(3, 4)
c3 = c1 - c2
print(f'{c1.real} + {c1.imag}j - {c2.real} + {c2.imag}j = {c3.real} + {c3.imag}j')

Output:

command
C:\Users\username>python sub.py
1 + 2j - 3 + 4j = -2 + -2j
command
C:\Users\username>python sub.py
1 + 2j - 3 + 4j = -2 + -2j

In the above example, we have defined the __sub__()__sub__() method in the ComplexComplex class. The __sub__()__sub__() method returns a ComplexComplex object. We have created two ComplexComplex objects named c1c1 and c2c2. We have subtracted the c1c1 and c2c2 objects using the -- operator. The output shows that the __sub__()__sub__() method is called when the -- operator is used with two ComplexComplex objects.

mul() Method

The __mul__()__mul__() method is called when the ** operator is used with two objects. It is used to multiply two objects. Let’s see how to use the __mul__()__mul__() method in Python.

mul.py
class Complex:
    def __init__(self, real, imag):
        self.real = real
        self.imag = imag
 
    def __mul__(self, other):
        return Complex(self.real * other.real, self.imag * other.imag)
 
c1 = Complex(1, 2)
c2 = Complex(3, 4)
c3 = c1 * c2
print(f'{c1.real} + {c1.imag}j * {c2.real} + {c2.imag}j = {c3.real} + {c3.imag}j')
mul.py
class Complex:
    def __init__(self, real, imag):
        self.real = real
        self.imag = imag
 
    def __mul__(self, other):
        return Complex(self.real * other.real, self.imag * other.imag)
 
c1 = Complex(1, 2)
c2 = Complex(3, 4)
c3 = c1 * c2
print(f'{c1.real} + {c1.imag}j * {c2.real} + {c2.imag}j = {c3.real} + {c3.imag}j')

Output:

command
C:\Users\username>python mul.py
1 + 2j * 3 + 4j = 3 + 8j
command
C:\Users\username>python mul.py
1 + 2j * 3 + 4j = 3 + 8j

In the above example, we have defined the __mul__()__mul__() method in the ComplexComplex class. The __mul__()__mul__() method returns a ComplexComplex object. We have created two ComplexComplex objects named c1c1 and c2c2. We have multiplied the c1c1 and c2c2 objects using the ** operator. The output shows that the __mul__()__mul__() method is called when the ** operator is used with two ComplexComplex objects.

truediv() Method

The __truediv__()__truediv__() method is called when the // operator is used with two objects. It is used to divide two objects. Let’s see how to use the __truediv__()__truediv__() method in Python.

truediv.py
class Complex:
    def __init__(self, real, imag):
        self.real = real
        self.imag = imag
 
    def __truediv__(self, other):
        return Complex(self.real / other.real, self.imag / other.imag)
 
c1 = Complex(1, 2)
c2 = Complex(3, 4)
c3 = c1 / c2
print(f'{c1.real} + {c1.imag}j / {c2.real} + {c2.imag}j = {c3.real} + {c3.imag}j')
truediv.py
class Complex:
    def __init__(self, real, imag):
        self.real = real
        self.imag = imag
 
    def __truediv__(self, other):
        return Complex(self.real / other.real, self.imag / other.imag)
 
c1 = Complex(1, 2)
c2 = Complex(3, 4)
c3 = c1 / c2
print(f'{c1.real} + {c1.imag}j / {c2.real} + {c2.imag}j = {c3.real} + {c3.imag}j')

Output:

command
C:\Users\username>python truediv.py
1 + 2j / 3 + 4j = 0.3333333333333333 + 0.5j
command
C:\Users\username>python truediv.py
1 + 2j / 3 + 4j = 0.3333333333333333 + 0.5j

In the above example, we have defined the __truediv__()__truediv__() method in the ComplexComplex class. The __truediv__()__truediv__() method returns a ComplexComplex object. We have created two ComplexComplex objects named c1c1 and c2c2. We have divided the c1c1 and c2c2 objects using the // operator. The output shows that the __truediv__()__truediv__() method is called when the // operator is used with two ComplexComplex objects.

floordiv() Method

The __floordiv__()__floordiv__() method is called when the //// operator is used with two objects. It is used to divide two objects. Let’s see how to use the __floordiv__()__floordiv__() method in Python.

floordiv.py
class Complex:
    def __init__(self, real, imag):
        self.real = real
        self.imag = imag
 
    def __floordiv__(self, other):
        return Complex(self.real // other.real, self.imag // other.imag)
 
c1 = Complex(1, 2)
c2 = Complex(3, 4)
c3 = c1 // c2
print(f'{c1.real} + {c1.imag}j // {c2.real} + {c2.imag}j = {c3.real} + {c3.imag}j')
floordiv.py
class Complex:
    def __init__(self, real, imag):
        self.real = real
        self.imag = imag
 
    def __floordiv__(self, other):
        return Complex(self.real // other.real, self.imag // other.imag)
 
c1 = Complex(1, 2)
c2 = Complex(3, 4)
c3 = c1 // c2
print(f'{c1.real} + {c1.imag}j // {c2.real} + {c2.imag}j = {c3.real} + {c3.imag}j')

Output:

command
C:\Users\username>python floordiv.py
1 + 2j // 3 + 4j = 0 + 0j
command
C:\Users\username>python floordiv.py
1 + 2j // 3 + 4j = 0 + 0j

In the above example, we have defined the __floordiv__()__floordiv__() method in the ComplexComplex class. The __floordiv__()__floordiv__() method returns a ComplexComplex object. We have created two ComplexComplex objects named c1c1 and c2c2. We have divided the c1c1 and c2c2 objects using the //// operator. The output shows that the __floordiv__()__floordiv__() method is called when the //// operator is used with two ComplexComplex objects.

mod() Method

The __mod__()__mod__() method is called when the %% operator is used with two objects. It is used to find the remainder of two objects. Let’s see how to use the __mod__()__mod__() method in Python.

mod.py
class Complex:
    def __init__(self, real, imag):
        self.real = real
        self.imag = imag
 
    def __mod__(self, other):
        return Complex(self.real % other.real, self.imag % other.imag)
 
c1 = Complex(1, 2)
c2 = Complex(3, 4)
c3 = c1 % c2
print(f'{c1.real} + {c1.imag}j % {c2.real} + {c2.imag}j = {c3.real} + {c3.imag}j')
mod.py
class Complex:
    def __init__(self, real, imag):
        self.real = real
        self.imag = imag
 
    def __mod__(self, other):
        return Complex(self.real % other.real, self.imag % other.imag)
 
c1 = Complex(1, 2)
c2 = Complex(3, 4)
c3 = c1 % c2
print(f'{c1.real} + {c1.imag}j % {c2.real} + {c2.imag}j = {c3.real} + {c3.imag}j')

Output:

command
C:\Users\username>python mod.py
1 + 2j % 3 + 4j = 1 + 2j
command
C:\Users\username>python mod.py
1 + 2j % 3 + 4j = 1 + 2j

In the above example, we have defined the __mod__()__mod__() method in the ComplexComplex class. The __mod__()__mod__() method returns a ComplexComplex object. We have created two ComplexComplex objects named c1c1 and c2c2. We have found the remainder of the c1c1 and c2c2 objects using the %% operator. The output shows that the __mod__()__mod__() method is called when the %% operator is used with two ComplexComplex objects.

pow() Method

The __pow__()__pow__() method is called when the **** operator is used with two objects. It is used to find the power of two objects. Let’s see how to use the __pow__()__pow__() method in Python.

pow.py
class Number:
    def __init__(self, num):
        self.num = num
 
    def __pow__(self, other):
        return Number(self.num ** other.num)
 
n1 = Number(2)
n2 = Number(3)
n3 = n1 ** n2
print(f'{n1.num} ** {n2.num} = {n3.num}')
pow.py
class Number:
    def __init__(self, num):
        self.num = num
 
    def __pow__(self, other):
        return Number(self.num ** other.num)
 
n1 = Number(2)
n2 = Number(3)
n3 = n1 ** n2
print(f'{n1.num} ** {n2.num} = {n3.num}')

Output:

command
C:\Users\username>python pow.py
2 ** 3 = 8
command
C:\Users\username>python pow.py
2 ** 3 = 8

In the above example, we have defined the __pow__()__pow__() method in the NumberNumber class. The __pow__()__pow__() method returns a NumberNumber object. We have created two NumberNumber objects named n1n1 and n2n2. We have found the power of the n1n1 and n2n2 objects using the **** operator. The output shows that the __pow__()__pow__() method is called when the **** operator is used with two NumberNumber objects.

and() Method

The __and__()__and__() method is called when the && operator is used with two objects. It is used to perform the bitwise AND operation on two objects. Let’s see how to use the __and__()__and__() method in Python.

and.py
class Number:
    def __init__(self, num):
        self.num = num
 
    def __and__(self, other):
        return Number(self.num & other.num)
 
n1 = Number(2)
n2 = Number(3)
n3 = n1 & n2
print(f'{n1.num} & {n2.num} = {n3.num}')
and.py
class Number:
    def __init__(self, num):
        self.num = num
 
    def __and__(self, other):
        return Number(self.num & other.num)
 
n1 = Number(2)
n2 = Number(3)
n3 = n1 & n2
print(f'{n1.num} & {n2.num} = {n3.num}')

Output:

command
C:\Users\username>python and.py
2 & 3 = 2
command
C:\Users\username>python and.py
2 & 3 = 2

In the above example, we have defined the __and__()__and__() method in the NumberNumber class. The __and__()__and__() method returns a NumberNumber object. We have created two NumberNumber objects named n1n1 and n2n2. We have performed the bitwise AND operation on the n1n1 and n2n2 objects using the && operator. The output shows that the __and__()__and__() method is called when the && operator is used with two NumberNumber objects.

or() Method

The __or__()__or__() method is called when the || operator is used with two objects. It is used to perform the bitwise OR operation on two objects. Let’s see how to use the __or__()__or__() method in Python.

or.py
class Number:
    def __init__(self, num):
        self.num = num
 
    def __or__(self, other):
        return Number(self.num | other.num)
 
n1 = Number(2)
n2 = Number(3)
n3 = n1 | n2
print(f'{n1.num} | {n2.num} = {n3.num}')
or.py
class Number:
    def __init__(self, num):
        self.num = num
 
    def __or__(self, other):
        return Number(self.num | other.num)
 
n1 = Number(2)
n2 = Number(3)
n3 = n1 | n2
print(f'{n1.num} | {n2.num} = {n3.num}')

Output:

command
C:\Users\username>python or.py
2 | 3 = 3
command
C:\Users\username>python or.py
2 | 3 = 3

In the above example, we have defined the __or__()__or__() method in the NumberNumber class. The __or__()__or__() method returns a NumberNumber object. We have created two NumberNumber objects named n1n1 and n2n2. We have performed the bitwise OR operation on the n1n1 and n2n2 objects using the || operator. The output shows that the __or__()__or__() method is called when the || operator is used with two NumberNumber objects.

xor() Method

The __xor__()__xor__() method is called when the ^^ operator is used with two objects. It is used to perform the bitwise XOR operation on two objects. Let’s see how to use the __xor__()__xor__() method in Python.

xor.py
class Number:
    def __init__(self, num):
        self.num = num
 
    def __xor__(self, other):
        return Number(self.num ^ other.num)
 
n1 = Number(2)
n2 = Number(3)
n3 = n1 ^ n2
print(f'{n1.num} ^ {n2.num} = {n3.num}')
xor.py
class Number:
    def __init__(self, num):
        self.num = num
 
    def __xor__(self, other):
        return Number(self.num ^ other.num)
 
n1 = Number(2)
n2 = Number(3)
n3 = n1 ^ n2
print(f'{n1.num} ^ {n2.num} = {n3.num}')

Output:

command
C:\Users\username>python xor.py
2 ^ 3 = 1
command
C:\Users\username>python xor.py
2 ^ 3 = 1

In the above example, we have defined the __xor__()__xor__() method in the NumberNumber class. The __xor__()__xor__() method returns a NumberNumber object. We have created two NumberNumber objects named n1n1 and n2n2. We have performed the bitwise XOR operation on the n1n1 and n2n2 objects using the ^^ operator. The output shows that the __xor__()__xor__() method is called when the ^^ operator is used with two NumberNumber objects.

lt() Method

The __lt__()__lt__() method is called when the << operator is used with two objects. It is used to compare two objects. It returns TrueTrue if the first object is less than the second object. Otherwise, it returns FalseFalse. Let’s see how to use the __lt__()__lt__() method in Python.

lt.py
class Number:
    def __init__(self, num):
        self.num = num
 
    def __lt__(self, other):
        return self.num < other.num
 
n1 = Number(2)
n2 = Number(3)
print(f'{n1.num} < {n2.num} = {n1 < n2}')
print(f'{n2.num} < {n1.num} = {n2 < n1}')
lt.py
class Number:
    def __init__(self, num):
        self.num = num
 
    def __lt__(self, other):
        return self.num < other.num
 
n1 = Number(2)
n2 = Number(3)
print(f'{n1.num} < {n2.num} = {n1 < n2}')
print(f'{n2.num} < {n1.num} = {n2 < n1}')

Output:

command
C:\Users\username>python lt.py
2 < 3 = True
3 < 2 = False
command
C:\Users\username>python lt.py
2 < 3 = True
3 < 2 = False

In the above example, we have defined the __lt__()__lt__() method in the NumberNumber class. The __lt__()__lt__() method returns TrueTrue if the first object is less than the second object. Otherwise, it returns FalseFalse. We have created two NumberNumber objects named n1n1 and n2n2. We have compared the n1n1 and n2n2 objects using the << operator. The output shows that the __lt__()__lt__() method is called when the << operator is used with two NumberNumber objects.

le() Method

The __le__()__le__() method is called when the <=<= operator is used with two objects. It is used to compare two objects. It returns TrueTrue if the first object is less than or equal to the second object. Otherwise, it returns FalseFalse. Let’s see how to use the __le__()__le__() method in Python.

le.py
class Number:
    def __init__(self, num):
        self.num = num
 
    def __le__(self, other):
        return self.num <= other.num
 
n1 = Number(2)
n2 = Number(3)
print(f'{n1.num} <= {n2.num} = {n1 <= n2}')
print(f'{n2.num} <= {n1.num} = {n2 <= n1}')
print(f'{n1.num} <= {n1.num} = {n1 <= n1}')
le.py
class Number:
    def __init__(self, num):
        self.num = num
 
    def __le__(self, other):
        return self.num <= other.num
 
n1 = Number(2)
n2 = Number(3)
print(f'{n1.num} <= {n2.num} = {n1 <= n2}')
print(f'{n2.num} <= {n1.num} = {n2 <= n1}')
print(f'{n1.num} <= {n1.num} = {n1 <= n1}')

Output:

command
C:\Users\username>python le.py
2 <= 3 = True
3 <= 2 = False
2 <= 2 = True
command
C:\Users\username>python le.py
2 <= 3 = True
3 <= 2 = False
2 <= 2 = True

In the above example, we have defined the __le__()__le__() method in the NumberNumber class. The __le__()__le__() method returns TrueTrue if the first object is less than or equal to the second object. Otherwise, it returns FalseFalse. We have created two NumberNumber objects named n1n1 and n2n2. We have compared the n1n1 and n2n2 objects using the <=<= operator. The output shows that the __le__()__le__() method is called when the <=<= operator is used with two NumberNumber objects.

eq() Method

The __eq__()__eq__() method is called when the ==== operator is used with two objects. It is used to compare two objects. It returns TrueTrue if the first object is equal to the second object. Otherwise, it returns FalseFalse. Let’s see how to use the __eq__()__eq__() method in Python.

eq.py
class Number:
    def __init__(self, num):
        self.num = num
 
    def __eq__(self, other):
        return self.num == other.num
 
n1 = Number(2)
n2 = Number(3)
print(f'{n1.num} == {n2.num} = {n1 == n2}')
print(f'{n2.num} == {n1.num} = {n2 == n1}')
print(f'{n1.num} == {n1.num} = {n1 == n1}')
eq.py
class Number:
    def __init__(self, num):
        self.num = num
 
    def __eq__(self, other):
        return self.num == other.num
 
n1 = Number(2)
n2 = Number(3)
print(f'{n1.num} == {n2.num} = {n1 == n2}')
print(f'{n2.num} == {n1.num} = {n2 == n1}')
print(f'{n1.num} == {n1.num} = {n1 == n1}')

Output:

command
C:\Users\username>python eq.py
2 == 3 = False
3 == 2 = False
2 == 2 = True
command
C:\Users\username>python eq.py
2 == 3 = False
3 == 2 = False
2 == 2 = True

In the above example, we have defined the __eq__()__eq__() method in the NumberNumber class. The __eq__()__eq__() method returns TrueTrue if the first object is equal to the second object. Otherwise, it returns FalseFalse. We have created two NumberNumber objects named n1n1 and n2n2. We have compared the n1n1 and n2n2 objects using the ==== operator. The output shows that the __eq__()__eq__() method is called when the ==== operator is used with two NumberNumber objects.

ne() Method

The __ne__()__ne__() method is called when the !=!= operator is used with two objects. It is used to compare two objects. It returns TrueTrue if the first object is not equal to the second object. Otherwise, it returns FalseFalse. Let’s see how to use the __ne__()__ne__() method in Python.

ne.py
class Number:
    def __init__(self, num):
        self.num = num
 
    def __ne__(self, other):
        return self.num != other.num
 
n1 = Number(2)
n2 = Number(3)
print(f'{n1.num} != {n2.num} = {n1 != n2}')
print(f'{n2.num} != {n1.num} = {n2 != n1}')
print(f'{n1.num} != {n1.num} = {n1 != n1}')
ne.py
class Number:
    def __init__(self, num):
        self.num = num
 
    def __ne__(self, other):
        return self.num != other.num
 
n1 = Number(2)
n2 = Number(3)
print(f'{n1.num} != {n2.num} = {n1 != n2}')
print(f'{n2.num} != {n1.num} = {n2 != n1}')
print(f'{n1.num} != {n1.num} = {n1 != n1}')

Output:

command
C:\Users\username>python ne.py
2 != 3 = True
3 != 2 = True
2 != 2 = False
command
C:\Users\username>python ne.py
2 != 3 = True
3 != 2 = True
2 != 2 = False

In the above example, we have defined the __ne__()__ne__() method in the NumberNumber class. The __ne__()__ne__() method returns TrueTrue if the first object is not equal to the second object. Otherwise, it returns FalseFalse. We have created two NumberNumber objects named n1n1 and n2n2. We have compared the n1n1 and n2n2 objects using the !=!= operator. The output shows that the __ne__()__ne__() method is called when the !=!= operator is used with two NumberNumber objects.

gt() Method

The __gt__()__gt__() method is called when the >> operator is used with two objects. It is used to compare two objects. It returns TrueTrue if the first object is greater than the second object. Otherwise, it returns FalseFalse. Let’s see how to use the __gt__()__gt__() method in Python.

gt.py
class Number:
    def __init__(self, num):
        self.num = num
 
    def __gt__(self, other):
        return self.num > other.num
 
n1 = Number(2)
n2 = Number(3)
print(f'{n1.num} > {n2.num} = {n1 > n2}')
print(f'{n2.num} > {n1.num} = {n2 > n1}')
print(f'{n1.num} > {n1.num} = {n1 > n1}')
gt.py
class Number:
    def __init__(self, num):
        self.num = num
 
    def __gt__(self, other):
        return self.num > other.num
 
n1 = Number(2)
n2 = Number(3)
print(f'{n1.num} > {n2.num} = {n1 > n2}')
print(f'{n2.num} > {n1.num} = {n2 > n1}')
print(f'{n1.num} > {n1.num} = {n1 > n1}')

Output:

command
C:\Users\username>python gt.py
2 > 3 = False
3 > 2 = True
2 > 2 = False
command
C:\Users\username>python gt.py
2 > 3 = False
3 > 2 = True
2 > 2 = False

In the above example, we have defined the __gt__()__gt__() method in the NumberNumber class. The __gt__()__gt__() method returns TrueTrue if the first object is greater than the second object. Otherwise, it returns FalseFalse. We have created two NumberNumber objects named n1n1 and n2n2. We have compared the n1n1 and n2n2 objects using the >> operator. The output shows that the __gt__()__gt__() method is called when the >> operator is used with two NumberNumber objects.

ge() Method

The __ge__()__ge__() method is called when the >=>= operator is used with two objects. It is used to compare two objects. It returns TrueTrue if the first object is greater than or equal to the second object. Otherwise, it returns FalseFalse. Let’s see how to use the __ge__()__ge__() method in Python.

ge.py
class Number:
    def __init__(self, num):
        self.num = num
 
    def __ge__(self, other):
        return self.num >= other.num
 
n1 = Number(2)
n2 = Number(3)
print(f'{n1.num} >= {n2.num} = {n1 >= n2}')
print(f'{n2.num} >= {n1.num} = {n2 >= n1}')
print(f'{n1.num} >= {n1.num} = {n1 >= n1}')
ge.py
class Number:
    def __init__(self, num):
        self.num = num
 
    def __ge__(self, other):
        return self.num >= other.num
 
n1 = Number(2)
n2 = Number(3)
print(f'{n1.num} >= {n2.num} = {n1 >= n2}')
print(f'{n2.num} >= {n1.num} = {n2 >= n1}')
print(f'{n1.num} >= {n1.num} = {n1 >= n1}')

Output:

command
C:\Users\username>python ge.py
2 >= 3 = False
3 >= 2 = True
2 >= 2 = True
command
C:\Users\username>python ge.py
2 >= 3 = False
3 >= 2 = True
2 >= 2 = True

In the above example, we have defined the __ge__()__ge__() method in the NumberNumber class. The __ge__()__ge__() method returns TrueTrue if the first object is greater than or equal to the second object. Otherwise, it returns FalseFalse. We have created two NumberNumber objects named n1n1 and n2n2. We have compared the n1n1 and n2n2 objects using the >=>= operator. The output shows that the __ge__()__ge__() method is called when the >=>= operator is used with two NumberNumber objects.

getitem() Method

The __getitem__()__getitem__() method is called when an item is accessed using the [][] operator. It is used to access an item of an object. Let’s see how to use the __getitem__()__getitem__() method in Python.

getitem.py
class Store:
    def __init__(self, items):
        self.items = items
 
    def __getitem__(self, index):
        return self.items[index]
 
store1 = Store(['Apple', 'Banana', 'Orange'])
print(store1[0])
print(store1[1])
print(store1[2])
getitem.py
class Store:
    def __init__(self, items):
        self.items = items
 
    def __getitem__(self, index):
        return self.items[index]
 
store1 = Store(['Apple', 'Banana', 'Orange'])
print(store1[0])
print(store1[1])
print(store1[2])

Output:

command
C:\Users\username>python getitem.py
Apple
Banana
Orange
command
C:\Users\username>python getitem.py
Apple
Banana
Orange

In the above example, we have defined the __getitem__()__getitem__() method in the StoreStore class. The __getitem__()__getitem__() method returns an item of the itemsitems list. We have created an object named store1store1 of the StoreStore class. We have accessed the items of the store1store1 object using the [][] operator. The output shows that the __getitem__()__getitem__() method is called when an item is accessed using the [][] operator.

setitem() Method

The __setitem__()__setitem__() method is called when an item is assigned using the [][] operator. It is used to assign an item of an object. Let’s see how to use the __setitem__()__setitem__() method in Python.

setitem.py
class Store:
    def __init__(self, items):
        self.items = items
 
    def __setitem__(self, index, value):
        self.items[index] = value
 
store1 = Store(['Apple', 'Banana', 'Orange'])
store1[0] = 'Mango'
store1[1] = 'Grapes'
store1[2] = 'Watermelon'
print(store1[0])
print(store1[1])
print(store1[2])
setitem.py
class Store:
    def __init__(self, items):
        self.items = items
 
    def __setitem__(self, index, value):
        self.items[index] = value
 
store1 = Store(['Apple', 'Banana', 'Orange'])
store1[0] = 'Mango'
store1[1] = 'Grapes'
store1[2] = 'Watermelon'
print(store1[0])
print(store1[1])
print(store1[2])

Output:

command
C:\Users\username>python setitem.py
Mango
Grapes
Watermelon
command
C:\Users\username>python setitem.py
Mango
Grapes
Watermelon

In the above example, we have defined the __setitem__()__setitem__() method in the StoreStore class. The __setitem__()__setitem__() method assigns an item of the itemsitems list. We have created an object named store1store1 of the StoreStore class. We have assigned the items of the store1store1 object using the [][] operator. The output shows that the __setitem__()__setitem__() method is called when an item is assigned using the [][] operator.

delitem() Method

The __delitem__()__delitem__() method is called when an item is deleted using the deldel operator. It is used to delete an item of an object. Let’s see how to use the __delitem__()__delitem__() method in Python.

delitem.py
class Store:
    def __init__(self, items):
        self.items = items
 
    def __delitem__(self, index):
        del self.items[index]
 
store1 = Store(['Apple', 'Banana', 'Orange'])
del store1[0]
print(store1.items)
del store1[1]
print(store1.items)
delitem.py
class Store:
    def __init__(self, items):
        self.items = items
 
    def __delitem__(self, index):
        del self.items[index]
 
store1 = Store(['Apple', 'Banana', 'Orange'])
del store1[0]
print(store1.items)
del store1[1]
print(store1.items)

Output:

command
C:\Users\username>python delitem.py
['Banana', 'Orange']
['Banana']
command
C:\Users\username>python delitem.py
['Banana', 'Orange']
['Banana']

In the above example, we have defined the __delitem__()__delitem__() method in the StoreStore class. The __delitem__()__delitem__() method deletes an item of the itemsitems list. We have created an object named store1store1 of the StoreStore class. We have deleted the items of the store1store1 object using the deldel operator. The output shows that the __delitem__()__delitem__() method is called when an item is deleted using the deldel operator.

contains() Method

The __contains__()__contains__() method is called when the inin operator is used with two objects. It is used to check if an item is present in an object. It returns TrueTrue if the item is present in the object. Otherwise, it returns FalseFalse. Let’s see how to use the __contains__()__contains__() method in Python.

contains.py
class Store:
    def __init__(self, items):
        self.items = items
 
    def __contains__(self, item):
        return item in self.items
 
store1 = Store(['Apple', 'Banana', 'Orange'])
print('Apple' in store1)
print('Mango' in store1)
contains.py
class Store:
    def __init__(self, items):
        self.items = items
 
    def __contains__(self, item):
        return item in self.items
 
store1 = Store(['Apple', 'Banana', 'Orange'])
print('Apple' in store1)
print('Mango' in store1)

Output:

command
C:\Users\username>python contains.py
True
False
command
C:\Users\username>python contains.py
True
False

In the above example, we have defined the __contains__()__contains__() method in the StoreStore class. The __contains__()__contains__() method returns TrueTrue if the item is present in the object. Otherwise, it returns FalseFalse. We have created an object named store1store1 of the StoreStore class. We have checked if the items are present in the store1store1 object using the inin operator. The output shows that the __contains__()__contains__() method is called when the inin operator is used with two objects.

call() Method

The __call__()__call__() method is called when an object is called as a function. It is used to call an object as a function. Let’s see how to use the __call__()__call__() method in Python.

call.py
class Store:
    def __init__(self, items):
        self.items = items
 
    def __call__(self, item):
        return item in self.items
 
store1 = Store(['Apple', 'Banana', 'Orange'])
print(store1('Apple'))
print(store1('Mango'))
call.py
class Store:
    def __init__(self, items):
        self.items = items
 
    def __call__(self, item):
        return item in self.items
 
store1 = Store(['Apple', 'Banana', 'Orange'])
print(store1('Apple'))
print(store1('Mango'))

Output:

command
C:\Users\username>python call.py
True
False
command
C:\Users\username>python call.py
True
False

In the above example, we have defined the __call__()__call__() method in the StoreStore class. The __call__()__call__() method returns TrueTrue if the item is present in the object. Otherwise, it returns FalseFalse. We have created an object named store1store1 of the StoreStore class. We have called the store1store1 object as a function. The output shows that the __call__()__call__() method is called when an object is called as a function.

enter() & exit() Methods

The __enter__()__enter__() and __exit__()__exit__() methods are called when an object is used with the withwith statement. It is used to create a context manager. Let’s see how to use the __enter__()__enter__() and __exit__()__exit__() methods in Python.

enter_exit.py
class Store:
    def __init__(self, items):
        self.items = items
 
    def __enter__(self):
        print('Entering the Store')
        return self
 
    def __exit__(self, exc_type, exc_value, exc_traceback):
        print('Exiting the Store')
 
    def __contains__(self, item):
        return item in self.items
 
store1 = Store(['Apple', 'Banana', 'Orange'])
with store1 as store:
    print('Apple' in store)
    print('Mango' in store)
enter_exit.py
class Store:
    def __init__(self, items):
        self.items = items
 
    def __enter__(self):
        print('Entering the Store')
        return self
 
    def __exit__(self, exc_type, exc_value, exc_traceback):
        print('Exiting the Store')
 
    def __contains__(self, item):
        return item in self.items
 
store1 = Store(['Apple', 'Banana', 'Orange'])
with store1 as store:
    print('Apple' in store)
    print('Mango' in store)

Output:

command
C:\Users\username>python enter_exit.py
Entering the Store
True
False
Exiting the Store
command
C:\Users\username>python enter_exit.py
Entering the Store
True
False
Exiting the Store

In the above example, we have defined the __enter__()__enter__() and __exit__()__exit__() methods in the StoreStore class. The __enter__()__enter__() method is called when the withwith statement is used with an object. The __exit__()__exit__() method is called when the withwith statement is exited. We have created an object named store1store1 of the StoreStore class. We have used the store1store1 object with the withwith statement. The output shows that the __enter__()__enter__() and __exit__()__exit__() methods are called when an object is used with the withwith statement.

iter() Method

The __iter__()__iter__() method is called when an object is iterated using the forfor loop. It is used to iterate over an object. Let’s see how to use the __iter__()__iter__() method in Python.

iter.py
class Store:
    def __init__(self, items):
        self.items = items
 
    def __iter__(self):
        return iter(self.items)
 
store1 = Store(['Apple', 'Banana', 'Orange'])
for item in store1:
    print(item)
iter.py
class Store:
    def __init__(self, items):
        self.items = items
 
    def __iter__(self):
        return iter(self.items)
 
store1 = Store(['Apple', 'Banana', 'Orange'])
for item in store1:
    print(item)

Output:

command
C:\Users\username>python iter.py
Apple
Banana
Orange
command
C:\Users\username>python iter.py
Apple
Banana
Orange

In the above example, we have defined the __iter__()__iter__() method in the StoreStore class. The __iter__()__iter__() method returns an iterator object. We have created an object named store1store1 of the StoreStore class. We have iterated over the store1store1 object using the forfor loop. The output shows that the __iter__()__iter__() method is called when an object is iterated using the forfor loop.

next() Method

The __next__()__next__() method is called when the next()next() function is called on an iterator object. It is used to return the next item of an iterator object. Let’s see how to use the __next__()__next__() method in Python.

next.py
class Store:
    def __init__(self, items):
        self.items = items
        self.index = 0
 
    def __iter__(self):
        return self
 
    def __next__(self):
        if self.index >= len(self.items):
            raise StopIteration
        item = self.items[self.index]
        self.index += 1
        return item
 
store1 = Store(['Apple', 'Banana', 'Orange'])
print(next(store1))
print(next(store1))
print(next(store1))
print(next(store1))
next.py
class Store:
    def __init__(self, items):
        self.items = items
        self.index = 0
 
    def __iter__(self):
        return self
 
    def __next__(self):
        if self.index >= len(self.items):
            raise StopIteration
        item = self.items[self.index]
        self.index += 1
        return item
 
store1 = Store(['Apple', 'Banana', 'Orange'])
print(next(store1))
print(next(store1))
print(next(store1))
print(next(store1))

Output:

command
C:\Users\username>python next.py
Apple
Banana
Orange
Traceback (most recent call last):
  File "next.py", line 21, in <module>
    print(next(store1))
  File "next.py", line 12, in __next__
    raise StopIteration
StopIteration
command
C:\Users\username>python next.py
Apple
Banana
Orange
Traceback (most recent call last):
  File "next.py", line 21, in <module>
    print(next(store1))
  File "next.py", line 12, in __next__
    raise StopIteration
StopIteration

In the above example, we have defined the __next__()__next__() method in the StoreStore class. The __next__()__next__() method returns the next item of the itemsitems list. We have created an object named store1store1 of the StoreStore class. We have called the next()next() function on the store1store1 object. The output shows that the __next__()__next__() method is called when the next()next() function is called on an iterator object.

reversed() Method

The __reversed__()__reversed__() method is called when the reversed()reversed() function is called on an object. It is used to return a reversed iterator object. Let’s see how to use the __reversed__()__reversed__() method in Python.

reversed.py
class Store:
    def __init__(self, items):
        self.items = items
 
    def __reversed__(self):
        return reversed(self.items)
 
store1 = Store(['Apple', 'Banana', 'Orange'])
for item in reversed(store1):
    print(item)
reversed.py
class Store:
    def __init__(self, items):
        self.items = items
 
    def __reversed__(self):
        return reversed(self.items)
 
store1 = Store(['Apple', 'Banana', 'Orange'])
for item in reversed(store1):
    print(item)

Output:

command
C:\Users\username>python reversed.py
Orange
Banana
Apple
command
C:\Users\username>python reversed.py
Orange
Banana
Apple

In the above example, we have defined the __reversed__()__reversed__() method in the StoreStore class. The __reversed__()__reversed__() method returns a reversed iterator object. We have created an object named store1store1 of the StoreStore class. We have iterated over the store1store1 object using the forfor loop. The output shows that the __reversed__()__reversed__() method is called when the reversed()reversed() function is called on an object.

hash() Method

The __hash__()__hash__() method is called when the hash()hash() function is called on an object. It is used to return the hash value of an object. Let’s see how to use the __hash__()__hash__() method in Python.

hash.py
class Store:
    def __init__(self, items):
        self.items = items
 
    def __hash__(self):
        return hash(self.items)
 
store1 = Store(['Apple', 'Banana', 'Orange'])
print(hash(store1))
hash.py
class Store:
    def __init__(self, items):
        self.items = items
 
    def __hash__(self):
        return hash(self.items)
 
store1 = Store(['Apple', 'Banana', 'Orange'])
print(hash(store1))

Output:

command
C:\Users\username>python hash.py
-9223372036574775808
command
C:\Users\username>python hash.py
-9223372036574775808

In the above example, we have defined the __hash__()__hash__() method in the StoreStore class. The __hash__()__hash__() method returns the hash value of the itemsitems list. We have created an object named store1store1 of the StoreStore class. We have called the hash()hash() function on the store1store1 object. The output shows that the __hash__()__hash__() method is called when the hash()hash() function is called on an object.

bool() Method

The __bool__()__bool__() method is called when the bool()bool() function is called on an object. It is used to return the boolean value of an object. Let’s see how to use the __bool__()__bool__() method in Python.

bool.py
class Number:
    def __init__(self, num):
        self.num = num
 
    def __bool__(self):
        return bool(self.num)
 
n1 = Number(0)
n2 = Number(1)
print(bool(n1))
print(bool(n2))
bool.py
class Number:
    def __init__(self, num):
        self.num = num
 
    def __bool__(self):
        return bool(self.num)
 
n1 = Number(0)
n2 = Number(1)
print(bool(n1))
print(bool(n2))

Output:

command
C:\Users\username>python bool.py
False
True
command
C:\Users\username>python bool.py
False
True

In the above example, we have defined the __bool__()__bool__() method in the NumberNumber class. The __bool__()__bool__() method returns the boolean value of the numnum attribute. We have created two NumberNumber objects named n1n1 and n2n2. We have called the bool()bool() function on the n1n1 and n2n2 objects. The output shows that the __bool__()__bool__() method is called when the bool()bool() function is called on an object.

format() Method

The __format__()__format__() method is called when the format()format() function is called on an object. It is used to return the formatted string of an object. Let’s see how to use the __format__()__format__() method in Python.

format.py
class Number:
    def __init__(self, num):
        self.num = num
 
    def __format__(self, format_spec):
        return format(self.num, format_spec)
 
n1 = Number(2)
n2 = Number(3)
print(format(n1, 'b'))
print(format(n2, 'b'))
format.py
class Number:
    def __init__(self, num):
        self.num = num
 
    def __format__(self, format_spec):
        return format(self.num, format_spec)
 
n1 = Number(2)
n2 = Number(3)
print(format(n1, 'b'))
print(format(n2, 'b'))

Output:

command
C:\Users\username>python format.py
10
11
command
C:\Users\username>python format.py
10
11

In the above example, we have defined the __format__()__format__() method in the NumberNumber class. The __format__()__format__() method returns the formatted string of the numnum attribute. We have created two NumberNumber objects named n1n1 and n2n2. We have called the format()format() function on the n1n1 and n2n2 objects. The output shows that the __format__()__format__() method is called when the format()format() function is called on an object.

index() Method

The __index__()__index__() method is called when the index()index() function is called on an object. It is used to return the index of an object. Let’s see how to use the __index__()__index__() method in Python.

index.py
class Number:
    def __init__(self, num):
        self.num = num
 
    def __index__(self):
        return self.num
 
n1 = Number(2)
n2 = Number(3)
print(index(n1))
print(index(n2))
index.py
class Number:
    def __init__(self, num):
        self.num = num
 
    def __index__(self):
        return self.num
 
n1 = Number(2)
n2 = Number(3)
print(index(n1))
print(index(n2))

Output:

command
C:\Users\username>python index.py
2
3
command
C:\Users\username>python index.py
2
3

In the above example, we have defined the __index__()__index__() method in the NumberNumber class. The __index__()__index__() method returns the index of the numnum attribute. We have created two NumberNumber objects named n1n1 and n2n2. We have called the index()index() function on the n1n1 and n2n2 objects. The output shows that the __index__()__index__() method is called when the index()index() function is called on an object.

int() Method

The __int__()__int__() method is called when the int()int() function is called on an object. It is used to return the integer value of an object. Let’s see how to use the __int__()__int__() method in Python.

int.py
class Complex:
    def __init__(self, real, imag):
        self.real = real
        self.imag = imag
 
    def __int__(self):
        return int(self.real)
 
c1 = Complex(2.5, 3.5)
c2 = Complex(3.5, 4.5)
print(int(c1))
print(int(c2))
int.py
class Complex:
    def __init__(self, real, imag):
        self.real = real
        self.imag = imag
 
    def __int__(self):
        return int(self.real)
 
c1 = Complex(2.5, 3.5)
c2 = Complex(3.5, 4.5)
print(int(c1))
print(int(c2))

Output:

command
C:\Users\username>python int.py
2
3
command
C:\Users\username>python int.py
2
3

In the above example, we have defined the __int__()__int__() method in the ComplexComplex class. The __int__()__int__() method returns the integer value of the realreal attribute. We have created two ComplexComplex objects named c1c1 and c2c2. We have called the int()int() function on the c1c1 and c2c2 objects. The output shows that the __int__()__int__() method is called when the int()int() function is called on an object.

float() Method

The __float__()__float__() method is called when the float()float() function is called on an object. It is used to return the float value of an object. Let’s see how to use the __float__()__float__() method in Python.

float.py
class Complex:
    def __init__(self, real, imag):
        self.real = real
        self.imag = imag
 
    def __float__(self):
        return float(self.real)
 
c1 = Complex(2.5, 3.5)
c2 = Complex(3.5, 4.5)
print(float(c1))
print(float(c2))
float.py
class Complex:
    def __init__(self, real, imag):
        self.real = real
        self.imag = imag
 
    def __float__(self):
        return float(self.real)
 
c1 = Complex(2.5, 3.5)
c2 = Complex(3.5, 4.5)
print(float(c1))
print(float(c2))

Output:

command
C:\Users\username>python float.py
2.5
3.5
command
C:\Users\username>python float.py
2.5
3.5

In the above example, we have defined the __float__()__float__() method in the ComplexComplex class. The __float__()__float__() method returns the float value of the realreal attribute. We have created two ComplexComplex objects named c1c1 and c2c2. We have called the float()float() function on the c1c1 and c2c2 objects. The output shows that the __float__()__float__() method is called when the float()float() function is called on an object.

complex() Method

The __complex__()__complex__() method is called when the complex()complex() function is called on an object. It is used to return the complex value of an object. Let’s see how to use the __complex__()__complex__() method in Python.

complex.py
class Number:
    def __init__(self, num):
        self.num = num
 
    def __complex__(self):
        return complex(self.num)
 
n1 = Number(2)
n2 = Number(3)
print(complex(n1))
print(complex(n2))
complex.py
class Number:
    def __init__(self, num):
        self.num = num
 
    def __complex__(self):
        return complex(self.num)
 
n1 = Number(2)
n2 = Number(3)
print(complex(n1))
print(complex(n2))

Output:

command
C:\Users\username>python complex.py
(2+0j)
(3+0j)
command
C:\Users\username>python complex.py
(2+0j)
(3+0j)

In the above example, we have defined the __complex__()__complex__() method in the NumberNumber class. The __complex__()__complex__() method returns the complex value of the numnum attribute. We have created two NumberNumber objects named n1n1 and n2n2. We have called the complex()complex() function on the n1n1 and n2n2 objects. The output shows that the __complex__()__complex__() method is called when the complex()complex() function is called on an object.

round() Method

The __round__()__round__() method is called when the round()round() function is called on an object. It is used to return the rounded value of an object. Let’s see how to use the __round__()__round__() method in Python.

round.py
class Number:
    def __init__(self, num):
        self.num = num
 
    def __round__(self):
        return round(self.num)
 
n1 = Number(2.5)
n2 = Number(3.5)
print(round(n1))
print(round(n2))
round.py
class Number:
    def __init__(self, num):
        self.num = num
 
    def __round__(self):
        return round(self.num)
 
n1 = Number(2.5)
n2 = Number(3.5)
print(round(n1))
print(round(n2))

Output:

command
C:\Users\username>python round.py
2
4
command
C:\Users\username>python round.py
2
4

In the above example, we have defined the __round__()__round__() method in the NumberNumber class. The __round__()__round__() method returns the rounded value of the numnum attribute. We have created two NumberNumber objects named n1n1 and n2n2. We have called the round()round() function on the n1n1 and n2n2 objects. The output shows that the __round__()__round__() method is called when the round()round() function is called on an object.

floor() Method

The __floor__()__floor__() method is called when the math.floor()math.floor() function is called on an object. It is used to return the floor value of an object. Let’s see how to use the __floor__()__floor__() method in Python.

floor.py
import math
 
class Number:
    def __init__(self, num):
        self.num = num
 
    def __floor__(self):
        return math.floor(self.num)
 
n1 = Number(2.5)
n2 = Number(3.5)
print(math.floor(n1))
print(math.floor(n2))
floor.py
import math
 
class Number:
    def __init__(self, num):
        self.num = num
 
    def __floor__(self):
        return math.floor(self.num)
 
n1 = Number(2.5)
n2 = Number(3.5)
print(math.floor(n1))
print(math.floor(n2))

Output:

command
C:\Users\username>python floor.py
2
3
command
C:\Users\username>python floor.py
2
3

In the above example, we have defined the __floor__()__floor__() method in the NumberNumber class. The __floor__()__floor__() method returns the floor value of the numnum attribute. We have created two NumberNumber objects named n1n1 and n2n2. We have called the math.floor()math.floor() function on the n1n1 and n2n2 objects. The output shows that the __floor__()__floor__() method is called when the math.floor()math.floor() function is called on an object.

ceil() Method

The __ceil__()__ceil__() method is called when the math.ceil()math.ceil() function is called on an object. It is used to return the ceil value of an object. Let’s see how to use the __ceil__()__ceil__() method in Python.

ceil.py
import math
 
class Number:
    def __init__(self, num):
        self.num = num
 
    def __ceil__(self):
        return math.ceil(self.num)
 
n1 = Number(2.5)
n2 = Number(3.5)
print(math.ceil(n1))
print(math.ceil(n2))
ceil.py
import math
 
class Number:
    def __init__(self, num):
        self.num = num
 
    def __ceil__(self):
        return math.ceil(self.num)
 
n1 = Number(2.5)
n2 = Number(3.5)
print(math.ceil(n1))
print(math.ceil(n2))

Output:

command
C:\Users\username>python ceil.py
3
4
command
C:\Users\username>python ceil.py
3
4

In the above example, we have defined the __ceil__()__ceil__() method in the NumberNumber class. The __ceil__()__ceil__() method returns the ceil value of the numnum attribute. We have created two NumberNumber objects named n1n1 and n2n2. We have called the math.ceil()math.ceil() function on the n1n1 and n2n2 objects. The output shows that the __ceil__()__ceil__() method is called when the math.ceil()math.ceil() function is called on an object.

trunc() Method

The __trunc__()__trunc__() method is called when the math.trunc()math.trunc() function is called on an object. It is used to return the truncated value of an object. Let’s see how to use the __trunc__()__trunc__() method in Python.

trunc.py
import math
 
class Number:
    def __init__(self, num):
        self.num = num
 
    def __trunc__(self):
        return math.trunc(self.num)
 
n1 = Number(2.5)
n2 = Number(3.5)
print(math.trunc(n1))
print(math.trunc(n2))
trunc.py
import math
 
class Number:
    def __init__(self, num):
        self.num = num
 
    def __trunc__(self):
        return math.trunc(self.num)
 
n1 = Number(2.5)
n2 = Number(3.5)
print(math.trunc(n1))
print(math.trunc(n2))

Output:

command
C:\Users\username>python trunc.py
2
3
command
C:\Users\username>python trunc.py
2
3

In the above example, we have defined the __trunc__()__trunc__() method in the NumberNumber class. The __trunc__()__trunc__() method returns the truncated value of the numnum attribute. We have created two NumberNumber objects named n1n1 and n2n2. We have called the math.trunc()math.trunc() function on the n1n1 and n2n2 objects. The output shows that the __trunc__()__trunc__() method is called when the math.trunc()math.trunc() function is called on an object.

pos() Method

The __pos__()__pos__() method is called when the ++ operator is used with an object. It is used to return the positive value of an object. Let’s see how to use the __pos__()__pos__() method in Python.

pos.py
class Number:
    def __init__(self, num):
        self.num = num
 
    def __pos__(self):
        return abs(self.num)
 
n1 = Number(-2)
n2 = Number(3)
print(+n1)
print(+n2)
pos.py
class Number:
    def __init__(self, num):
        self.num = num
 
    def __pos__(self):
        return abs(self.num)
 
n1 = Number(-2)
n2 = Number(3)
print(+n1)
print(+n2)

Output:

command
C:\Users\username>python pos.py
2
3
command
C:\Users\username>python pos.py
2
3

In the above example, we have defined the __pos__()__pos__() method in the NumberNumber class. The __pos__()__pos__() method returns the positive value of the numnum attribute. We have created two NumberNumber objects named n1n1 and n2n2. We have used the ++ operator with the n1n1 and n2n2 objects. The output shows that the __pos__()__pos__() method is called when the ++ operator is used with an object.

neg() Method

The __neg__()__neg__() method is called when the -- operator is used with an object. It is used to return the negative value of an object. Let’s see how to use the __neg__()__neg__() method in Python.

neg.py
class Number:
    def __init__(self, num):
        self.num = num
 
    def __neg__(self):
        return -self.num
 
n1 = Number(2)
n2 = Number(3)
print(-n1)
print(-n2)
neg.py
class Number:
    def __init__(self, num):
        self.num = num
 
    def __neg__(self):
        return -self.num
 
n1 = Number(2)
n2 = Number(3)
print(-n1)
print(-n2)

Output:

command
C:\Users\username>python neg.py
-2
-3
command
C:\Users\username>python neg.py
-2
-3

In the above example, we have defined the __neg__()__neg__() method in the NumberNumber class. The __neg__()__neg__() method returns the negative value of the numnum attribute. We have created two NumberNumber objects named n1n1 and n2n2. We have used the -- operator with the n1n1 and n2n2 objects. The output shows that the __neg__()__neg__() method is called when the -- operator is used with an object.

abs() Method

The __abs__()__abs__() method is called when the abs()abs() function is called on an object. It is used to return the absolute value of an object. Let’s see how to use the __abs__()__abs__() method in Python.

abs.py
class Number:
    def __init__(self, num):
        self.num = num
 
    def __abs__(self):
        return abs(self.num)
 
n1 = Number(-2)
n2 = Number(3)
print(abs(n1))
print(abs(n2))
abs.py
class Number:
    def __init__(self, num):
        self.num = num
 
    def __abs__(self):
        return abs(self.num)
 
n1 = Number(-2)
n2 = Number(3)
print(abs(n1))
print(abs(n2))

Output:

command
C:\Users\username>python abs.py
2
3
command
C:\Users\username>python abs.py
2
3

In the above example, we have defined the __abs__()__abs__() method in the NumberNumber class. The __abs__()__abs__() method returns the absolute value of the numnum attribute. We have created two NumberNumber objects named n1n1 and n2n2. We have called the abs()abs() function on the n1n1 and n2n2 objects. The output shows that the __abs__()__abs__() method is called when the abs()abs() function is called on an object.

invert() Method

The __invert__()__invert__() method is called when the ~~ operator is used with an object. It is used to return the inverted value of an object. Let’s see how to use the __invert__()__invert__() method in Python.

invert.py
class Number:
    def __init__(self, num):
        self.num = num
 
    def __invert__(self):
        return ~self.num
 
n1 = Number(2)
n2 = Number(3)
print(~n1)
print(~n2)
invert.py
class Number:
    def __init__(self, num):
        self.num = num
 
    def __invert__(self):
        return ~self.num
 
n1 = Number(2)
n2 = Number(3)
print(~n1)
print(~n2)

Output:

command
C:\Users\username>python invert.py
-3
-4
command
C:\Users\username>python invert.py
-3
-4

In the above example, we have defined the __invert__()__invert__() method in the NumberNumber class. The __invert__()__invert__() method returns the inverted value of the numnum attribute. We have created two NumberNumber objects named n1n1 and n2n2. We have used the ~~ operator with the n1n1 and n2n2 objects. The output shows that the __invert__()__invert__() method is called when the ~~ operator is used with an object.

str() Method VS repr() Method

The __str__()__str__() and __repr__()__repr__() methods are called when the str()str() and repr()repr() functions are called on an object. Both methods are used to return the string representation of an object. The __str__()__str__() method is called when the str()str() function is called on an object. It is used to return the informal string representation of an object. The __repr__()__repr__() method is called when the repr()repr() function is called on an object. It is used to return the official string representation of an object. This both can use to print the object. Let’s see how to use the __str__()__str__() and __repr__()__repr__() methods in Python.

str_repr.py
class Number:
    def __init__(self, num):
        self.num = num
 
    def __str__(self):
        print('Calling __str__ method')
        return str(self.num)
 
    def __repr__(self):
        print('Calling __repr__ method')
        return repr(self.num)
 
n = Number(2)
print(str(n))
print(repr(n))
print(n)
str_repr.py
class Number:
    def __init__(self, num):
        self.num = num
 
    def __str__(self):
        print('Calling __str__ method')
        return str(self.num)
 
    def __repr__(self):
        print('Calling __repr__ method')
        return repr(self.num)
 
n = Number(2)
print(str(n))
print(repr(n))
print(n)

Output:

command
C:\Users\username>python str_repr.py
Calling __str__ method
2
Calling __repr__ method
2
Calling __str__ method
2
command
C:\Users\username>python str_repr.py
Calling __str__ method
2
Calling __repr__ method
2
Calling __str__ method
2

In the above example, we have defined the __str__()__str__() and __repr__()__repr__() methods in the NumberNumber class. The __str__()__str__() method returns the informal string representation of the numnum attribute. The __repr__()__repr__() method returns the official string representation of the numnum attribute. We have created an object named nn of the NumberNumber class. We have called the str()str() and repr()repr() functions on the nn object. We have also printed the nn object. The output shows that the __str__()__str__() and __repr__()__repr__() methods are called when the str()str() and repr()repr() functions are called on an object. The output also shows that the __str__()__str__() method is called when an object is printed.

Multiple Dispatch

Multiple dispatch is a feature of some programming languages in which a function or method can be dynamically dispatched based on the run time (dynamic) type or, in the more general case, some other attribute of more than one of its arguments. This is a generalization of single dispatch polymorphism and subclass polymorphism, where a method call is dynamically dispatched based on the class of one object.

In python, we can use the multipledispatchmultipledispatch library to implement multiple dispatch. Let’s see how to use the multipledispatchmultipledispatch library in Python.

command
C:\Users\username>pip install multipledispatch
command
C:\Users\username>pip install multipledispatch

In the above example, we have installed the multipledispatchmultipledispatch library using the pippip command.

main.py
from multipledispatch import dispatch
 
class Calculator:
    @dispatch(int, int)
    def add(self, a, b):
        print("Two Numbers Argument")
        return a + b
 
    @dispatch(int, int, int)
    def add(self, a, b, c):
        print("Three Numbers Argument")
        return a + b + c
 
    @dispatch(float, float)
    def add(self, a, b):
        print("Two Floats Argument")
        return a + b
 
    @dispatch(str, str)
    def add(self, a, b):
        print("Two Strings Argument")
        return a + b
 
calculator = Calculator()
print(calculator.add(2, 3))
print(calculator.add(2, 3, 4))
print(calculator.add(2.5, 3.5))
print(calculator.add('Hello', 'World'))
main.py
from multipledispatch import dispatch
 
class Calculator:
    @dispatch(int, int)
    def add(self, a, b):
        print("Two Numbers Argument")
        return a + b
 
    @dispatch(int, int, int)
    def add(self, a, b, c):
        print("Three Numbers Argument")
        return a + b + c
 
    @dispatch(float, float)
    def add(self, a, b):
        print("Two Floats Argument")
        return a + b
 
    @dispatch(str, str)
    def add(self, a, b):
        print("Two Strings Argument")
        return a + b
 
calculator = Calculator()
print(calculator.add(2, 3))
print(calculator.add(2, 3, 4))
print(calculator.add(2.5, 3.5))
print(calculator.add('Hello', 'World'))

Output:

command
C:\Users\username>python main.py
Two Numbers Argument
5
Three Numbers Argument
9
Two Floats Argument
6.0
Two Strings Argument
HelloWorld
command
C:\Users\username>python main.py
Two Numbers Argument
5
Three Numbers Argument
9
Two Floats Argument
6.0
Two Strings Argument
HelloWorld

In the above example, we have defined the CalculatorCalculator class. We have defined the add()add() method in the CalculatorCalculator class. We have used the @dispatch@dispatch decorator to define the add()add() method with different arguments. We have created an object named calculatorcalculator of the CalculatorCalculator class. We have called the add()add() method of the calculatorcalculator object with different arguments. The output shows that the add()add() method is called based on the arguments. If the arguments are intint and intint, then the add()add() method with two intint arguments is called. If the arguments are intint, intint, and intint, then the add()add() method with three intint arguments is called. If the arguments are floatfloat and floatfloat, then the add()add() method with two floatfloat arguments is called. If the arguments are strstr and strstr, then the add()add() method with two strstr arguments is called.

Conclusion

In this tutorial, we have learned about the magic methods in Python. We have learned about the magic methods for operator overloading, comparison, container, and type conversion. We have also learned about the multiple dispatch in Python. Now you can use the magic methods in Python to implement the operator overloading, comparison, container, and type conversion. You can also use the multiple dispatch in Python to implement the multiple dispatch. For more information on the magic methods in Python, you can refer to the official documentation of the magic methods in Python. For more tutorials on Python, you can visit Python Central Hub.

Was this page helpful?

Let us know how we did