[Tutor] class questions
Hugo Arts
hugo.yoshi at gmail.com
Sun Jun 27 21:30:46 CEST 2010
On Sun, Jun 27, 2010 at 6:47 PM, Shashwat Anand
<anand.shashwat at gmail.com> wrote:
> <snip>
>>
>> The problem of the MRO isn't that it doesn't work, it's that it causes
>> behavior that is unintuitive. In my example, We would expect D.x to be
>> equal to C.x (since D inherits from C, and C overrides the x method).
>> However, this is not the case. This is what the problem is with the
>> old MRO system, not so much that it doesn't work at all, but the
>> results aren't consistent with what you'd expect from multiple
>> inheritance.
>>
>
> I tried your example in python2.6
>>>> class A:
> ... def x(self): return "in A"
> ...
>>>> class B(A): pass
> ...
>>>> class C(A):
> ... def x(self): return "in C"
> ...
>>>> class D(B, C): pass
> ...
>>>> o = D()
>>>> o.x()
> 'in A'
> If MRO was in python2.3, the output of o.x() should be 'in C'. It works fine
> on 3.2alpha though.
>
You forgot that the example as is still uses the old MRO, even in 2.3.
This is because these classes are still classic-style classes. To use
the new MRO you will have to use new-style classes (A must inherit
from object). In 3.x it will of course work fine, since everything
without explicit inheritance inherits from object.
Hugo
More information about the Tutor
mailing list