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