1

When I try to get the id of a bound method from the shell I get different ids. I would expect to see id(c.myFunc) returning the same value every time. Could someone please explain why id(c.myFunc) is changing?

 >>> class MyClass(object):
...     def myFunc():
...         pass
...         
>>> c = MyClass()
>>> c.myFunc
<bound method MyClass.myFunc of <__main__.MyClass object at 0x0000000031CFD7B8>>
>>> id(c.myFunc)
837301736L
>>> id(c.myFunc)
833022784L
>>> id(c.myFunc)
837301520L
ewall198
  • 11
  • 1
  • 1
    Didn't you forgot a `self`? The reason this happens is that it attaches `c` to the `myfunc` each time. You thus construct a "new" function each time you call `c.myFunc`. `MyClass.myFunc` on the other hand does not bind the `self` and thus the `id` will be the same. – Willem Van Onsem Aug 09 '17 at 15:24

0 Answers0