Consider the following interaction:
>>> def func():
... pass
...
>>> id(func)
2138387939184
>>> def func():
... x = 5
...
>>> id(func)
2138390016064
>>> def func():
... pass
...
>>> id(func)
2138387939184
>>>
>>> def func2():
... pass
...
>>> id(func2)
2138390016064
So you can see that after func
is redefined to its original form: pass
, it received the same memory address. That got me thinking that when I define another function, no matter its name, if the body (and parameters list) are the same, it will be bound to the same address as well, but when I define func2
as only pass
, it gets another address.
Can someone explain this?
EDIT
My assumption was that the reason that when I defined
...
def func():
pass
in the second time it received the same id, was that this function definition already exists in that memory address, so the interpreter doesn't have to recreate it. But given the following:
>>> def func():
... pass
...
>>> id(func)
1829442649968
>>> def func():
... x = 5
...
>>> id(func)
1829448396864
>>> def func():
... y = 10
...
>>> id(func)
1829442649968
clearly shows that this thesis was wrong. It assigned the func
object the same id only because it is now free.