Interesting results with the '+=' increment operator [duplicate]
Asked Answered
F

4

5

I had learned that n = n + v and n += v are the same. Until this;

def assign_value(n, v):
    n += v
    print(n)

l1 = [1, 2, 3]
l2 = [4, 5, 6]

assign_value(l1, l2)
print(l1)

The output will be:

[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6]

Now when I use the expanded version:

def assign_value(n, v):
    n = n + v
    print(n)

l1 = [1, 2, 3]
l2 = [4, 5, 6]

assign_value(l1, l2)
print(l1)

The output will be:

[1, 2, 3, 4, 5, 6]
[1, 2, 3]

Using the += has a different result with the fully expanded operation. What is causing this?

Fax answered 4/1, 2023 at 11:38 Comment(2)
In python this is because __add__ and __iadd__ can be implemented independently and differently.Earlie
I had learned that n = n + v and n += v are the same. They are not the same.Biblioclast
R
2

It may seem counter-intuitive, but they are not always the same. In fact,

  • a = a + b means a = a.__add__(b), creating a new object
  • a += b means a = a.__iadd__(b), mutating the object

__iadd__, if absent, defaults to the __add__, but it also can (and it does, in the case of lists) mutate the original object in-place.

Rupertruperta answered 4/1, 2023 at 11:50 Comment(0)
C
7

Thats because in the first implementation you are editing the list n itself (and therefore the changes still apply when leaving the function), while on the other implementation you are creating a new temporary list with the same name, so when you leave the function the new list disappears and the variable n is linked to the original list.

the += operator works similarly to x=x+y for immutable objects (since they always create new objects), but for mutable objects such as lists they work differently. x=x+y creats a new object x while x+=y edits the current object.

Carny answered 4/1, 2023 at 11:44 Comment(0)
R
2

It may seem counter-intuitive, but they are not always the same. In fact,

  • a = a + b means a = a.__add__(b), creating a new object
  • a += b means a = a.__iadd__(b), mutating the object

__iadd__, if absent, defaults to the __add__, but it also can (and it does, in the case of lists) mutate the original object in-place.

Rupertruperta answered 4/1, 2023 at 11:50 Comment(0)
F
1

This works on how python treats objects and passes variables into functions. Basically - in first example (with += ) You are passing n and v into function by "pass-by-assignment" So n gets modified and it will be also modified out of function scope.

In second example - n is reassigned inside of the function to a new list. Which is not seen outside of the function.

Fagan answered 4/1, 2023 at 11:48 Comment(0)
M
1

In your 1st code. You changes list n itself see the below image..!

enter image description here

In your 2nd code. you just created a temporary list which is cleared when function call ends.. see the below images..!

enter image description here

In the next step when function ends the temporary list clear!! enter image description here

Measurement answered 4/1, 2023 at 11:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.