This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: different method, but id function return same value.
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: lgj1993, methane, steven.daprano
Priority: normal Keywords:

Created on 2019-03-01 10:01 by lgj1993, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg336912 - (view) Author: lgj (lgj1993) Date: 2019-03-01 10:01
>>> class A:
...     def a(self):
...         return 0
...     def a1(self):
...         return 1
...
>>> a =A()
>>> id(a.a)
4316559496
>>> id(a.a1)
4316559496
>>> id(a)
4318155272
It' seems oops , according to the id function source code.
here is the description of builtin_id function in Python/bltinmodule.c
/* 
Return the identity of an object.

This is guaranteed to be unique among simultaneously existing objects.
(CPython uses the object's memory address.)
[clinic start generated code]*
/
It seems should return different value, but id(a.a) as same as 
id(a.a1), Is it a bug?
msg336915 - (view) Author: Inada Naoki (methane) * (Python committer) Date: 2019-03-01 10:24
a.a creates temporal method object.  After id(a.a), it is removed.
So a.a and a.a1 are not "simultaneously existing objects" in your case.

Try this:

x = a.a
y = a.a1
id(x), id(y)

x and y are "simultaneously existing objects" now.
msg336916 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2019-03-01 13:30
bugs.python.org seems to be down at the moment, so please forgive me if 
this ticket has already been closed and I'm repeating what has already 
been said.

> This is guaranteed to be unique among simultaneously existing objects.

Note the *simultaneously existing* comment. Since the method objects a.a 
and a.a1 don't exist simultaneously, the interpreter is allowed to reuse 
the same ID for each.

(P.S. please, next time use less confusing names!)

Your code does this:

- fetch a.a, which returns a new method object;
- print the ID of that method object;
- throw away and garbage collect the method object;
- fetch a.a1, which returns a new method object;
- print the ID of that method object which happens
  to get the same value as the previous object;
- throw away and garbage collect the method object.

You can see the difference if you do this:

spam = a.a
eggs = a.a1
print(id(spam), id(eggs))

and you will get two different IDs.
History
Date User Action Args
2022-04-11 14:59:11adminsetgithub: 80337
2019-03-01 13:30:00steven.dapranosetnosy: + steven.daprano
messages: + msg336916
2019-03-01 10:24:01methanesetstatus: open -> closed

nosy: + methane
messages: + msg336915

resolution: not a bug
stage: resolved
2019-03-01 10:01:47lgj1993create