Function parameter is reference?

37 Views Asked by At
def add_list(p):
    p = p + [1]

p1 = [1, 2, 3]
add_list(p1)
print p1


res:[1, 2, 3]

BUT

def add_list(p):
    p += [1]

p1 = [1, 2, 3]
add_list(p1)
print p1

res:[1, 2, 3, 1]

I don't know why, can someone explain this? What's the main difference between them?

0

There are 0 best solutions below