0% found this document useful (0 votes)
12 views23 pages

Lecture 16

The document discusses operator overloading in Python by using classes for different bird types like Duck and Swan. It shows how to overload operators like + to add two Duck objects and return a Swan object. It also lists all the operators that can be overloaded in Python like arithmetic, comparison, bitwise and augmented assignment operators.

Uploaded by

bsmt22098
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views23 pages

Lecture 16

The document discusses operator overloading in Python by using classes for different bird types like Duck and Swan. It shows how to overload operators like + to add two Duck objects and return a Swan object. It also lists all the operators that can be overloaded in Python like arithmetic, comparison, bitwise and augmented assignment operators.

Uploaded by

bsmt22098
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Lecture 16

Duck and other birds?


Concept:
Operator Overloading
Operator Overloading
• Binary and unary opoerators in a language have a defined function

• 2+3
• 5*7
• “Hello”*7
Operator Overloading
• d = Duck()
• e = Duck()

• What will you get with

• d+e ????
Operator Overloading
• d = Duck()
• e = Duck()

• What will you get with

• d+e ????
• Time to make it happen
Operator Overloading
• Operator overloading is a process of making your native operators
work for objects they’re not defined for
Why only
duck?
Operator Overloading
class Duck:
def __init__(self):
print("Duck created")
def print(self):
print("I'm a duck")
Operator Overloading
class Swan:
def __init__(self):
print("Swan created")
def print(self):
print("I'm a Swan")
Operator Overloading
class Duck:
def __init__(self):
print("Duck created")
def print(self):
print("I'm a duck")
def __add__(self, other):
if isinstance(other, Duck):
return Swan()
Operator Overloading
d = Duck()
e = Duck()

f = d+e

d.print()
e.print()
f.print()
Operator Overloading
d = Duck()
e = Duck()

f = d+e

d.print()
e.print()
f.print()
Super
Cool!
Operator Overloading
class Duck:
def __init__(self):
print("Duck created")
def print(self):
print("I'm a duck")
def __add__(self, other):
if isinstance(other, Duck):
return Swan()
else: ?
Which operators are supported?
+ __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)
Which operators are supported?

< __lt__(self, other)


> __gt__(self, other)
<= __le__(self, other)
>= __ge__(self, other)
== __eq__(self, other)
!= __ne__(self, other)
Which operators are supported?

< __lt__(self, other)


> __gt__(self, other)
<= __le__(self, other)
>= __ge__(self, other)
== __eq__(self, other)
!= __ne__(self, other)
Which operators are supported?
-= __isub__(self, other)
+= __iadd__(self, other)
*= __imul__(self, other)
/= __idiv__(self, other)
//= __ifloordiv__(self, other)
%= __imod__(self, other)
**= __ipow__(self, other)
>>= __irshift__(self, other)
<<= __ilshift__(self, other)
&= __iand__(self, other)
|= __ior__(self, other)
^= __ixor__(self, other)
• Imagine the objects
• Duck
• Goose
• Eagle
• Dragon
• Duck + Duck is a Goose

• Goose + Goose is an Eagle

• Eagle + Eagle is a fire breathing dragon


• Duck < Goose

• Goose < Eagle

• Eagle < Fire Breathing Dragon


• Goose > Duck

• Eagle > Goose

• Fire Breathing Dragon > Eagle


Making the chain work ?
d = Duck()
e = Duck()
f = Duck()
g = Duck()

x = d+e+f+g

You might also like