-1

In python, for example if I write:

def f(x,y):
    x.append(y) #1
    x += [y]        #2
    x = x+[y] #3
    return x
x=[]
y=3
z=f(x,y)

Only the operations 1 and 2 modify the value of x (that will be [3,3]) Why? There's a general rule?

user254087
  • 51
  • 5

1 Answers1

0

Python's list class defines __add__, which controls the behaviour of x + y, and it defines __iadd__, which controls the behaviour of x += y. The implementations of these specify that x + y creates a new list, and x += y modifies the list x. Presumably these seemed like the most likely intention of people using those operations.

khelwood
  • 55,782
  • 14
  • 81
  • 108