Is list pass by value or by reference? [duplicate]
2
This question already has an answer here:
How do I pass a variable by reference?
24 answers
Consider the following code, at first glance it does the same thing, but the result is different, sometimes it seems list is pass by value, sometimes list seems pass by reference: lst = [1, 2] def f(lst): # lst = lst + [3] # seems pass by value # lst += [3] # strange! same as above but seems pass by reference lst = lst.append(3) # seems pass by reference return lst f(lst) print(lst) can anyone tell me what is going on?
python list
share | improve this question