Has comparison of instancemethods changed between python 2.5 and 2.4?

Frank Niessink frank at niessink.com
Fri Dec 15 18:57:49 EST 2006


Fredrik Lundh:
> Frank Niessink wrote:
> 
>  > However, the instance the two methods belong to are different, i.e.
>  > id(callback) returns different values for the two methods.
> 
> are you using the *object* as the callback?  otherwise, that sentence 
> doesn't make much sense; 

You are right, that sentence doesn't make much sense. The callbacks are 
instance methods, not the objects themselves. What I meant to say is 
that both the instances are different and the callbacks are different 
(i.e. their id is different), but the callbacks still compare as equal. 
So I have to instance methods where: id(instancemethod1) != 
id(instancemethod2) but instancemethod1 == instancemethod2. However, 
your explanation below explains why the id of the instance methods may 
be different.

> bound methods are generated on the fly, and the 
> identity may or may not be reused, depending on how things are garbage 
> collected:
> 
>  >>> f.bar
> <bound method foo.bar of <__main__.foo object at 0x009D1BD0>>
>  >>> id(f.bar)
> 10322248
>  >>> id(f.bar)
> 10322328
>  >>> id(f.bar)
> 10322328
>  >>> id(f.bar)
> 10322328
>  >>> id(f.bar), id(f.bar)
> (10322328, 10322328)
>  >>> map(id, (f.bar, f.bar, f.bar))
> [10322328, 10322248, 10322368]

OK, so that explains why the id of (two references to the same) 
instancemethod(s) may differ. But I'm still confused why two 
instancemethods of two different instances can compare as equal.

Thanks, Frank



More information about the Python-list mailing list