The idea is, similar to with ints, you can use =+ instead of base = base +additive. This would make sense becuase you can already do base(list) = base(list) + additive(list) and i works perfectly fine. Just an idea tho.
Did you try +=
instead?
4 Likes
And be careful it you are doing x =+ y
for x
and y
instances of int
and interpreting that executing as working.
What is doing is computing +y
, the unary operator +
on y
, resulting in y
and assigning that value to x
.
1 Like
There is no =+
operator. =+
means =
(ordinary assignment) followed by +
(unary +). x =+ y
means to compute +y
(this will normally be the same thing as y
for numeric types, and will normally give an error for non-numeric types), and then assign that to x
.
You are thinking of +=
. You can use this with both lists and ints. In fact, it is special-cased for lists: it will use the same logic as the extend
method, so that the original list is modified - it does not create a copy. This is important:
>>> a = [1]
>>> b = a # both names for the same object
>>> a = a + [2] # now `a` becomes a different list; `b` is unchanged
>>> b
[1]
>>> a = [1]
>>> b = a
>>> a += [2] # now that list is modified; `b` is still a name for it, so it is changed
>>> b
[1, 2]