Something strange with 'object'
Dan P
dan at eevolved.com
Fri Feb 1 14:18:29 EST 2002
Boris Boutillier wrote:
> hi all,
>
> I made a python program and had a bug that turn out to be a problem with
> 'object' heritage, here is a tiny script that reproduce the bug :
> Buggous version :
>
>>class a(object):
>> def __add__(self,other):
>> print 'a.add'
>>class b(a):
>> def __radd__(self,other):
>> print 'b.radd'
>>x = b()
>>y = b()
>>x.__class__ = a
>>x+y
> b.radd
>
> This means that a.add hasn't been called for x.
>
> If you remove the object heritage, the buggous behaviour disappear :
>>class a:
>> def __add__(self,other):
>> print 'a.add'
>>class b(a):
>> def __radd__(self,other):
>> print 'b.radd'
>>x = b()
>>y = b()
>>x.__class__ = a
>>x+y
> a.add
>
> which is what you want.
>
> Is this a real bug ? or did I miss something about use of 'object' ? I
> tried to find documentation in python reference guide but found nothing
> helpfull.
> Someone's got an idea ?
My guess is that it's the __mro__. You changed the instance's class, but
it's method resolution order (stored in the __mro__ attribute) still has
class b as its first element.
Tell me what you find out,
Dan
> Boris Boutillier
More information about the Python-list
mailing list