When is "i += x" different from "i = i + x" in Python?
when is eid2020
when is eade
when is m
when is spring
when is easter
when is eid 2020
when is mother's day
I was told that
+= can have different effects than the standard notation of
i = i +. Is there a case in which
i += 1 would be different from
i = i + 1?
This depends entirely on the object
From an API perspective,
__iadd__ is supposed to be used for modifying mutable objects in place (returning the object which was mutated) whereas
__add__ should return a new instance of something. For immutable objects, both methods return a new instance, but
__iadd__ will put the new instance in the current namespace with the same name that the old instance had. This is why
i = 1 i += 1
seems to increment
i. In reality, you get a new integer and assign it "on top of"
i -- losing one reference to the old integer. In this case,
i += 1 is exactly the same as
i = i + 1. But, with most mutable objects, it's a different story:
As a concrete example:
a = [1, 2, 3] b = a b += [1, 2, 3] print a #[1, 2, 3, 1, 2, 3] print b #[1, 2, 3, 1, 2, 3]
a = [1, 2, 3] b = a b = b + [1, 2, 3] print a #[1, 2, 3] print b #[1, 2, 3, 1, 2, 3]
notice how in the first example, since
a reference the same object, when I use
b, it actually changes
a sees that change too -- After all, it's referencing the same list). In the second case however, when I do
b = b + [1, 2, 3], this takes the list that
b is referencing and concatenates it with a new list
[1, 2, 3]. It then stores the concatenated list in the current namespace as
b -- With no regard for what
b was the line before.
1In the expression
x + y, if
x.__add__ isn't implemented or if
y have different types, then
x + y tries to call
y.__radd__(x). So, in the case where you have
foo_instance += bar_instance
Foo doesn't implement
__iadd__ then the result here is the same as
foo_instance = bar_instance.__radd__(bar_instance, foo_instance)
2In the expression
foo_instance + bar_instance,
bar_instance.__radd__ will be tried before
foo_instance.__add__ if the type of
bar_instance is a subclass of the type of
issubclass(Bar, Foo)). The rational for this is because
Bar is in some sense a "higher-level" object than
Bar should get the option of overriding
When is a pandemic over?, Explore the three strategies governments and communities can use to contain and end a Duration: 5:53 Posted: 1 Jun 2020 When definition is - at what time. How to use when in a sentence.
Under the covers,
i += 1 does something like this:
try: i = i.__iadd__(1) except AttributeError: i = i.__add__(1)
i = i + 1 does something like this:
i = i.__add__(1)
This is a slight oversimplification, but you get the idea: Python gives types a way to handle
+= specially, by creating an
__iadd__ method as well as an
The intention is that mutable types, like
list, will mutate themselves in
__iadd__ (and then return
self, unless you're doing something very tricky), while immutable types, like
int, will just not implement it.
>>> l1 =  >>> l2 = l1 >>> l1 +=  >>> l2 
l2 is the same object as
l1, and you mutated
l1, you also mutated
>>> l1 =  >>> l2 = l1 >>> l1 = l1 +  >>> l2 
Here, you didn't mutate
l1; instead, you created a new list,
l1 + , and rebound the name
l1 to point at it, leaving
l2 pointing at the original list.
+= version, you were also rebinding
l1, it's just that in that case you were rebinding it to the same
list it was already bound to, so you can usually ignore that part.)
Tax credits: your payment dates, If you do not have a claim form, contact HM Revenue and Customs ( HMRC ). Find out when your tax credits payment is and how much you'll get paid. Usage Note: In informal style when is often used after forms of be in definitions: A dilemma is when you don't know which way to turn. Although useful, this construction is widely regarded as incorrect or unsuitable for formal discourse.
Here is an example that directly compares
i += x with
i = i + x:
def foo(x): x = x +  def bar(x): x +=  c =  foo(c); # c is not changed bar(c); # c is changed to [27, 42]
Check the MOT status of a vehicle, Enter a vehicle's details to see whether it has an MOT certificate and when it runs out. This service is also available in Welsh (Cymraeg). Start now on the vehicle� When definition, at what time or period? how long ago? how soon?: When are they to arrive? When did the Roman Empire exist? See more.
When is my waste collected?, When is my waste collected? To find your next collection date, or to download your calendar enter your post code in the box below Easter, considered the greatest feast day in the Christian calendar, is a moveable feast, which means that it falls on a different date each year.Easter always falls on a Sunday, but Easter Sunday can be as early as March 22 and as late as April 25.
A Discovery of Witches season 2 release date, When is A Discovery of Witches season 2 release date? Everything you need to know about the returning supernatural romance starring� Amazon has not yet announced the official dates of Prime Day 2020, but reports say it has been pushed back to September. Here's everything we know.
When is my bin collection day?, Find out your bin day - when your rubbish, waste and recycling is collected. When is Thanksgiving? USA Thanksgiving date 2019 and beyond. Plan ahead with Thanksgiving dates for 2019-2025, plus past Thanksgiving dates 2005-2018.
extend()in case of lists.
- @AshwiniChaudhary That's a pretty subtle distinction, considering that
True. Many developers may not notice that
id(i)changes for one operation, but not the other.
- @kojiro -- While it's a subtle distinction, I think it is an important one.
- @mgilson it is important, and so I felt it needed an explanation. :)
- Related question regarding differences between the two in Java: stackoverflow.com/a/7456548/245966
__iadd__if it exists, and falls back to adding and rebinding otherwise. That's why
i = 1; i += 1works even though there's no
int.__iadd__. But other than that minor nit, great explanations.
- @abarnert -- I always assumed that
__add__. I'm glad to have learned something new today :).
- @abarnert -- I suppose maybe to be complete,
x + ycalls
x.__add__doesn't exist (or returns
yare of different types)
- If you really want to be completist, you'd have to mention that the "if it exists" bit goes through the usual getattr mechanisms, except for some quirks with classic classes, and for types implemented in the C API it instead looks for either
sq_inplace_concat, and those C API functions have stricter requirements than the Python dunder methods, and… But I don't think that's relevant to the answer. The main distinction is that
+=tries to do an in-place add before falling back to acting like
+, which I think you've already explained.
- Yeah, I suppose you're right ... Although I could just fall back on the stance that the C API isn't part of python. It's part of Cpython :-P
__add__in the event of an
i += 1that calls
- errr... Yeah, that's what I meant. Interesting. I didn't realize that was done automatically.
- The first attempt is actually
i = i.__iadd__(1)-
iaddcan modify the object in place, but doesn't have to, and so is expected to return the result in either case.
- Note that this means that
AttributeError, but it can't rebind the result… so
i=1; operator.iadd(i, 1)returns 2 and leaves
1. Which is a bit confusing.