0

I was told that the python function is passing by reference, here is an example

def append_element(some_list, element):
    some_list.append(element)

data = [3, 1, 2]
append_element(data, 4)

print(data)
[3,1,2,4]

So the original data has changed. What makes me feel confused is

y = sorted(data)
print(y)
[1,2,3,4]
print(data)
[3,1,2,4]

If python function is indeed passing by reference, then why after this function call sorted(), the data did not change?

KevinKim
  • 1,382
  • 3
  • 18
  • 34
  • 2
    Python does not pass by reference. – user2357112 Jan 26 '17 at 21:37
  • I was reading the book "Python for Data Analysis" in the appendix, page 390. Also, if it not pass by reference, then how to explain the first example? – KevinKim Jan 26 '17 at 21:44
  • Python uses a parameter passing model that's quite common these days, yet doesn't have a good name. It's sometimes referred to as "call by object" or (especially for Java folks) "pass by value, but you have to keep in mind that the value you're passing is really a reference". I usually point people to the following explanation: http://nedbatchelder.com/text/names.html – user2357112 Jan 26 '17 at 22:12
  • I have started using the term [call by sharing](https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sharing). – juanpa.arrivillaga Jan 26 '17 at 22:24
  • @user2357112 That link is pretty good. Thanks! – KevinKim Jan 27 '17 at 02:33
  • @user2357112 there is one thing in that link that I am a little bit confused is when he talk about mutable and immutable. Say `A=[1,2]` and `B=A; C=A`, now if `A=A+[3]`, then you will see A become [1,2,3] while B,C are still [1,2], but if now you do `C.append(3)`, then both B,C becomes [1,2,3]. This example is hard to fit into his theory. A is a list, mutable, initiall, A,B,C are all name tag to the object [1,2], then why A = A+[3] is making a new list and assign name tag A to refer to it, but C.append(3) is mutate the original list [1,2] to [1,2,3]? – KevinKim Jan 27 '17 at 02:56
  • @ftxx: It's not clear what part of that you expected to behave differently. After `A=[1, 2]; B=A; C=A`, the variables A, B, and C all refer to a single list. `A+[3]` builds a new list (without affecting the original), and `A=A+[3]` makes the A variable refer to that new list. In contrast, `C.append(3)` retrieves and mutates the list that C refers to. – user2357112 Jan 27 '17 at 03:45
  • @user2357112 So it seems that if you use assignment and `+` operations, it is like open a new memory on the heap to store new values without affecting the old values; if you use method operation `.somefunction()`, then it is changing the old values not open new memory on the heap. Is that correct. Is that correct? But if you put the variable (or name tag) into the argument of a function, e.g., `sorted(L)`, then I guess it depends on the exactly command inside the function in order to determine whether a new memory is opened on the heap or just changing the old values. Right? – KevinKim Jan 27 '17 at 16:36

1 Answers1

2

The output from sorted() is a different, brand new list. It's not the same as the argument.

recursive
  • 83,943
  • 34
  • 151
  • 241