1

I initialize variable a and b. a has id=559599568, b has id=559251864.

I then assign b to a in the function change(), expecting a points to b's location and a has b's value.

However, what I found is a indeed points to b's location within the function (as you can see, they all have id=559251864 when print inside the function change), but once the function returns, a points back to it's original id which is 559599568.

Why a points back to its original memory id once returns? I thought python is passing by reference if it is a mutable object? Please correct me.

a = pd.DataFrame(range(3))
b = pd.DataFrame(range(5))
def change(origin,new):
    print id(origin)
    print id(new)
    origin = new
    print id(origin)
    print id(new)
change(a,b)

Out[24]: change(a,b)
559599568
559251864
559251864
559251864
a
Out[25]:
   0
0  0
1  1
2  2
b
Out[26]:
   0
0  0
1  1
2  2
3  3
4  4
id(a)
Out[27]: 559599568L
id(b)
Out[28]: 559251864L
martineau
  • 119,623
  • 25
  • 170
  • 301
Lisa
  • 4,126
  • 12
  • 42
  • 71
  • 1
    Every variable is, technically, a reference to another object. But the variables themselves are passed by value; you can't change the value of a variable by passing it as an argument to a function. You may want to read https://nedbatchelder.com/text/names.html. – chepner May 23 '18 at 17:43
  • 1
    Python parameter passing is the same with mutable or immutable objects. See [Facts and myths about Python names and values](https://nedbatchelder.com/text/names.html). – user2357112 May 23 '18 at 17:45
  • Python is **never** call-by-reference, and the mutability of the object is irrelevant to the evaluation strategy. The Python evaluation strategy is call-by-sharing, which is the same strategy as Java (for reference types) and most modern lanuages, e.g. Javascript, Ruby etc. https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sharing – juanpa.arrivillaga May 23 '18 at 17:49

1 Answers1

1

Yes, Python is passing a reference to the object (call by sharing). But your assignment origin = new is also assigning the reference. So this makes origin and new refer to the same object within the scope of the function, but doesn't change anything outside the function.

Thomas
  • 174,939
  • 50
  • 355
  • 478
  • 3
    I think it muddies the waters to say that Python is [pass-by-reference](https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sharing). It is technically call-by-sharing, which is distinct from call-by-reference. Indeed, the characteristic property of call-by-reference is that assignments are seen by the caller. People tend to use "call-by-reference" rather loosely, though... – juanpa.arrivillaga May 23 '18 at 17:48
  • @juanpa.arrivillaga Thanks, interesting, learned something today. Updated my answer. – Thomas May 23 '18 at 17:50
  • 2
    Thanks for fixing that, it's one of my pet peeves :) Again, I think most people would understand "call-by-reference" to mean "passing a reference to the object", but I like to keep terminology straight, and I believe that drift in this regards just makes everything more confusing – juanpa.arrivillaga May 23 '18 at 17:50