[Baypiggies] Question on Python's MRO...
Minesh Patel
minesh at gmail.com
Wed Jan 4 14:20:17 EST 2017
If the order was mixin2->mixin1->class A then wouldn't it have printed "In
mixin2.foo()"?
On Wed, Jan 4, 2017 at 10:07 AM, Braun Brelin <bbrelin at gmail.com> wrote:
> What I'm confused about is the following:
>
> #!/usr/bin/env python3
> class mixin1(object):
> def foo(self):
> print ("In mixin1.foo()")
>
> class mixin2 (object):
> def foo(self):
> print ("In mixin2.foo()")
>
> class A(object):
> def __init__(self):
> print ("In class A")
>
> class B(A,mixin1,mixin2):
> def __init__(self):
> self.foo()
>
>
> myB = B()
>
> ------------------------------------------------------------
> --------------------------------------------------
> The output of this program is:
> In mixin1.foo()
>
> So, it seems to be doing method resolution from right to left. No?
>
> Braun
>
>
>
> I've always understood that the class hierarchy when determining
> inheritance is that Python looks at class mixin2 first, then class mixin1,
> then the base class A. I.e. the order is right to left. A number of web
> site
>
> On Wed, Jan 4, 2017 at 7:37 PM, Guido van Rossum <guido at python.org> wrote:
>
>> C's MRO is (C, A, B, object), and method lookup happens in that order. So
>> if both A and B define a method m, but C doesn't, A.m gets used.
>>
>> For old-style classes the lookup order would still be (C, A, B).
>>
>> Maybe you can clarify *what* is happening from right to left in your
>> understanding? Code speaks!
>>
>> On Wed, Jan 4, 2017 at 9:30 AM, Braun Brelin <bbrelin at gmail.com> wrote:
>>
>>> Hi all,
>>>
>>> I'm trying to figure out how to understand Python's Method Resolution
>>> order.
>>> One of the things that's really confusing me is that from my
>>> understanding
>>> if I have a class declaration like this
>>>
>>> class C(A,B):
>>> ...
>>>
>>> Python does the inheritance order from right to left, yet all the
>>> tutorials on MRO
>>> start talking about inheritance from left to right.
>>>
>>> Is the right to left order a property of the older Python 2 style
>>> classes that didn't
>>> explicitly inherit from object?
>>>
>>> Thanks
>>>
>>>
>>>
>>> _______________________________________________
>>> Baypiggies mailing list
>>> Baypiggies at python.org
>>> To change your subscription options or unsubscribe:
>>> https://mail.python.org/mailman/listinfo/baypiggies
>>>
>>
>>
>>
>> --
>> --Guido van Rossum (python.org/~guido)
>>
>
>
> _______________________________________________
> Baypiggies mailing list
> Baypiggies at python.org
> To change your subscription options or unsubscribe:
> https://mail.python.org/mailman/listinfo/baypiggies
>
--
Thanks,
--Minesh
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/baypiggies/attachments/20170104/b9b1eba7/attachment-0001.html>
More information about the Baypiggies
mailing list