Overloading

1.Overloading:

By defining other special methods,you can specify the behaviour of operators on user-defined types.For example,If you define a method named__add__ for the Time class,you can use the + operator on Time objects.
Here how you use it:

>>>start=Time(9,45)
>>>duration=Time(1,35)
>>>print start + duration
11:20:00
Changing the behaviour of an operators so that it works with user-defined types is called operator overloading.
1.Example for Overloading: 
def product(a, b): 
p = a * b 
print(p) 
# Second product method 
# Takes three argument and print their 
# product 
def product(a, b, c): 
p = a * b*c 
print(p) 
# Uncommenting the below line shows an error 
# product(4, 5) 
# This line will call the second product method 
product(4, 5, 5) 
Output:
100
Translate »