Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMatthew Ison
5,989 PointsDelete __iadd__ and everything worked the same...
In this video it appears to be showing that iadd let's us store the numerical value in age(age += 1) and then add it to another number. However, I deleted the iadd function and age+=1, then age +5 gave the same output as when the iadd was in place.
So if it wasn't necessary for the example in the video, then what is an example where it's actually necessary?
3 Answers
Jeff Muday
Treehouse Moderator 28,720 PointsThis can be a little confusing at first.
__iadd__
is the "inplace addition" operator. It is different than the __add__
method which simply returns the RESULT of the addition to another object. But instead the __iadd__
is 2 operations with one operator... it evaluates first then and replaces itself with a new object.
Since the addition is "inplace" it means that the value of object is modified in place. Its counterparts are __isub__
and iconcat
for strings.
x = 3
# the iadd adds and modifies the object, 2 operations in one!
x += 1
print(x)
Craig Stephenson
2,490 PointsI agree with Matthew Ison. I also found the definition of iadd to not be necessary. Jeff, it is not that Matthew and I do not understand the purpose of iadd (i.e. addition and then assignment to the original variable), it is that in practice we have found it to not be necessary. Perhaps, we are using a later version of Python?
Craig Stephenson
2,490 PointsI see that this issue had already been addressed in an earlier post by Chris Freeman (May 15, 2019). It seems that the purpose of the iadd dunder method is to allow the addition to be performed in such a way that the result is of the correct type (NumString, not an integer or real), which is not the case for the implementation shown in the video.
Jeff Muday
Treehouse Moderator 28,720 PointsI haven't read Chris' response @chrisfreeman3, he's a smart guy for sure. Returning the correct type is a good use case for this.
Make sure you note this, as a programming interview may try to trip you up with this.
For practical purposes, __iadd__
makes the +=
operation work CORRECTLY-- that is, it DOES NOT need to create a NEW copy of the variable. The one that only implements the __add__
ends up replacing the original instance.
Think of it like this: normally, when you use the +
operator (handled by the __add__
method), it’s as if you’ve mixed two things together in a bowl and poured them into a fresh, new container. But with __iadd__
, Python is much more practical. Instead of fetching a new container, it just says, "Right, let’s keep this one. We’ll modify it right here." In terms of everyday life, it's like adding a few more steps to your pedometer without buying a whole new one each time. It’s all very efficient.
In many cases, the __add__
operator is just fine, but edge cases you would want the __iadd__
.
Does this matter? Sometimes.
TL;DR proof
class Point1:
# This is an implementation of the Point class, it can add objects to a third object
# but it is missing the ability to ACTUALLY modify the object in-place
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other):
# Adding the corresponding coordinates
return Point1(self.x + other.x, self.y + other.y)
def __repr__(self):
return f"Point1({self.x}, {self.y})"
class Point2:
# This is the original implementation of the Point class, it can modify the object in-place.
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other):
# Adding the corresponding coordinates
return Point2(self.x + other.x, self.y + other.y)
def __iadd__(self, other):
# Modifying the object in place by adding the coordinates
self.x += other.x
self.y += other.y
return self # Returning the modified object itself
def __repr__(self):
return f"Point2({self.x}, {self.y})"
# Example usage
p1 = Point1(2, 3)
print(f'Original: p1 = {p1}, id(p1): {id(p1)}')
p2 = Point1(4, 1)
p1 += p2 # This calls p1.__add__(p2)
# Note the id, it is different than the original
print(f'Inplace operation: {p1}, id(p1): {id(p1)}')
# Example usage
q1 = Point2(2, 3)
print(f'Original: q1 = {q1}, id(q1): {id(q1)}')
q2 = Point2(4, 1)
q1 += q2 # This calls p1.__iadd__(q2)
# Note the id, it is using the SAME memory location.
print(f'Inplace operation: {q1}, id(q1): {id(q1)}')
Here is a run of the program. It clearly shows the __add__
method produces a NEW memory location when used. The second with the __iadd__
method shows it used the SAME location.
(base) jeff@pop-os:~/projects/treehouse-dev$ python3 test_iadd.py
Original: p1 = Point1(2, 3), id(p1): 129052629523040
Inplace operation: Point1(6, 4), id(p1): 129052629523136
Original: q1 = Point2(2, 3), id(q1): 129052629523040
Inplace operation: Point2(6, 4), id(q1): 129052629523040