The id() of the parameter with the same name passed into a function is not necessarily changed

  Kiến thức lập trình

In Python, I was explaining to a friend how two variables can have the same name if one is passed as a parameter to a function.

For this, I tried:

def func(var):
    print(id(var), "inside function")
    var = var
    return var
var = 3
print(id(var), "outside function")
var = func(var)

I was not expecting the id()’s to be the same.
Furthermore, I tried:

from copy import deepcopy

def func(var):
    print(id(var), "1st inside function")
    
    var = deepcopy(var)
    
    print(id(var), "2nd inside function")
    return var

var = 3
print(id(var), "1st outside function")
var = func(var)
print(id(var), "2nd outside function")

And all the id() values are still the same.
I was expecting the id() to change somehow. This seems counterintuitive to me at the moment.
Can someone please explain this behavior?

LEAVE A COMMENT