[New-bugs-announce] [issue33685] Instances bound methods with different memory addresses but sharing same id

Marcelo Alves report at bugs.python.org
Tue May 29 12:28:20 EDT 2018


New submission from Marcelo Alves <marcelo.franco.alves at gmail.com>:

Different instances should have different bound method ids, since they have different memory addresses, isn’t it? Example:

I have a class and two instances:

    class MyClass:
       def something():
          pass
    
    a = MyClass()
    b = MyClass()

If we print `a.something` and `b.something`, we can see that they have different memory addresses:

# a.something
<bound method MyClass.something of <__main__.MyClass object at 0x103438588>>

# b.something
<bound method MyClass.something of <__main__.MyClass object at 0x10342add8>>

This clear indicates that they aren’t the same, and we can confirm that comparing both using the `is` operator:

>>> a.something is b.something
False

But the identity of both indicates that they are the same, according with the doc of `is` and `id`:

>>> id(a.something)
4350192008

>>> id(b.something)
4350192008

The documentation of `is` says:

The operators is and is not test for object identity: x is y is true if and only if x and y are the same object. x is not y yields the inverse truth value. [6]

ref: https://docs.python.org/2/reference/expressions.html#is

And the documentation of `id` says:

Return the “identity” of an object. This is an integer (or long 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.

CPython implementation detail: This is the address of the object in memory.

Thanks!

----------
components: Interpreter Core
messages: 318057
nosy: celicoo
priority: normal
severity: normal
status: open
title: Instances bound methods with different memory addresses but sharing same id
type: behavior
versions: Python 3.6

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue33685>
_______________________________________


More information about the New-bugs-announce mailing list