2182019 Python Operator Overlading ~The Python Guru
The Python Guru
Become a better python developer
Python Operator Overloading
(Sponsors) Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data
Science by completing interactive coding challenges and watching videos by expert instructors, Start
Now!
You have already seen you can use + operator for adding numbers and at the same time to concatenate
strings. Itis possible because + operators overloaded by both int classand str class. The operators
are actually methods defined in respective classes. Defining methods for operators is known as operator
overloading. For e.g. To use + operator with custom objects you need to define a method called __add__
Let's take an example to understand better
1 | import math
2
3 | class Circle:
4
5 def __init_(self, radius):
6 Self. _radius’= radius
?
8 def setRadius(self, radius):
9 self.__radius = radius
10
ul def getRadius(self):
2 return self._radius
B
“4 def areaCself):
45 return math.pi * self.__radius ** 2
16
7 def _add_(self, another_circle):
18 return CircleC self._cradius + another_circle._radius )
20 | cl = Circle(4)
21 | printcct.getRadius())
23 | «2 = Circle(s)
24 | print(c2.getRadiusc)
This website uses cookies to improve your experience, We'll assume you're ok with this, but you can opt-out if you
wish. Read More
hitpsuitnepytnonguru.comipytnon-operatr-overoading! 192iarzor9 Python Operator Overloading - The Python Guru
Expected Output:
14
2)5
319
run > openin @prepl.it
mainpy r
1 import math
2
3 class Circle:
4
5 def _init_(self, radius):
6 self._radius = radius
7
8 def setRadius(self, radius):
In the above example we have added _add___ method which allows use to use + operator to add two
circle objects. Inside the __add__ method we are creating a new object and returning it to the caller.
python has many other special methods like _add___, see the list below.
‘OPERATOR FUNCTION METHOD DESCRIPTION
+ --add__(self, other) Addition
* mul.
(self, other) Multiplication
(self, other) Subtraction
% —umod__(self, other) Remainder
/ ~-truediv__(self, other) Division
This website uses cookies to improve your experience, We'll assume you're ok with this, but you can opt-out if you
wish. Read More
htps:thepythonguru.convpython-operatar-overloading! 202182019
OPERATOR
>
Cindex]
in
len
str
yon Operator Overloading - The Python Gury
FUNCTION
—-eq_(self, other)
_ne_(self, other)
—-gt_(self, other)
—-ge_(self, other)
—-getitem__(self, index)
-_contains_(self, value)
—len__(self)
_str__(self)
METHOD DESCRIPTION
Equalto
Not equal to
Greater than
Greater than or equal to
Index operator
Check membership
‘The number of elements
The string representation
Program below is using some of the above mentioned functions to overload operators,
1
2
3
4
5
6
7
8
9
10
cre
12
13
14
15
16
v7
18
19
20
21
22
23
24
25
26
2
28
29
30
31
32
33
34
def
def
def
def
def
def
def
‘import math
class Circle:
setRadius(self, radius):
self.__radius ~ radius
getRadius(self):
return self._radius
areaCself):
return math.pi * self._radius ** 2
—-add__(self, another_circle):
radius + another_circle._radius )
return Circle( self
—-gt_(self, another_circle):
return self._radius > another_circle.__radius
—-lt_(self, another_circle):
return self._radius < another_circle.__radius
str.
self):
return "Circle with radius " + str(self.—radius)
cl = Circle(4)
print(cl.getRadius())
«2 = Circles)
print(c2.getRadius())
This website uses cookies to improve your experience, We'll assume you're ok with this, but you can opt-out if you
wish.
hitpsuitnepytnonguru.comipytnon-operatr-overoading!
Read More
a9
Music Theory - From Beginner To Expert - The Ultimate Step-By-Step Guide To Understanding and Learning Music Theory Effortlessly (Essential Learning Tools For Musicians Book 1)
Music Theory - From Beginner To Expert - The Ultimate Step-By-Step Guide To Understanding and Learning Music Theory Effortlessly (Essential Learning Tools For Musicians Book 1)