-1

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.

YoavKlein
  • 2,005
  • 9
  • 38
  • What exactly is unclear to you? – mkrieger1 Jun 16 '21 at 08:35
  • Are you asking *us* why *you* have made an incorrect *assumption*? – DeepSpace Jun 16 '21 at 08:36
  • Also, see https://stackoverflow.com/questions/52096582/how-unique-is-pythons-id – DeepSpace Jun 16 '21 at 08:41
  • 3
    "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" that is simply not true. ID's are unique *only for the lifetime of the object*. When you use the same name, the old function is no longer referenced, it is garbage collected, and the ID is free to be *re-used*. When or why that happens is an implementation detail of the privately managed heap – juanpa.arrivillaga Jun 16 '21 at 08:53

1 Answers1

0

cf the documentation for the builtin id function :

Return the “identity” of an object. This is an integer which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value.

You SHOULD not consider valid the id of objects that have been deleted.

Because you redefine your function func, you actually create a new object (functions are objects in Python) each time, that's why the id changed in the first place (because it is a new, different object, with a different id). That the same id was reused later is coincidental, it may or not happen, and it tells you nothing useful (except for garbage collection implementation details).

Comparing ids is safer when performed at the same time, for example if id(obj1) == id(obj2): print("we are sure it is the same object").

See also this very similar question : Why is the id of a Python class not unique when called quickly?

Lenormju
  • 4,078
  • 2
  • 8
  • 22