Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > MRO problems with diamond inheritance?

Reply
Thread Tools

MRO problems with diamond inheritance?

 
 
John Perks and Sarah Mount
Guest
Posts: n/a
 
      05-01-2005
Trying to create the "lopsided diamond" inheritance below:

>>> class B(object)ass
>>> class D1(B)ass
>>> class D2(D1)ass
>>> class D(D1, D2)ass

Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: Error when calling the metaclass bases
Cannot create a consistent method resolution
order (MRO) for bases D1, D2

Is this as intended? Especially since reversing the order makes it OK:

>>> class D(D2, D1)ass
>>> D.__mro__

(<class '__main__.D'>, <class '__main__.D2'>, <class '__main__.D1'>,
<class '__m
ain__.B'>, <type 'object'>)

Why should order of base classes matter? This only affects types,
old-style classes are unaffected.

The workaround to this problem (if it is a problem and not a feature
that I'm misunderstanding) is to put more-derived classes at the front
of the list. I ran into this with dynamically-created classes, and
sorting the bases list with the following comparator put them right:

def cmpMRO(x,y):
if x == y:
return 0
elif issubclass(x, y):
return -1
elif issubclass(y, x):
return +1
else:
return cmp(id(x), id(y))

Incidentally, is it safe to have an mro() method in a class, or is this
as reserved as the usual __reserved_words__? Does Python use mro() (as
opposed to __mro__) internally or anything?

Thanks

John



 
Reply With Quote
 
 
 
 
M.E.Farmer
Guest
Posts: n/a
 
      05-01-2005
Your answer lies somewhere in this page
http://www.python.org/2.2.2/descrintro.html
M.E.Farmer

 
Reply With Quote
 
 
 
 
Robert Dick
Guest
Posts: n/a
 
      05-01-2005
M.E.Farmer:
> Your answer lies somewhere in this page
> http://www.python.org/2.2.2/descrintro.html
> M.E.Farmer


delegate.py (use PyPI) may also be useful.

-Robert Dick-
 
Reply With Quote
 
Michele Simionato
Guest
Posts: n/a
 
      05-02-2005
> M.E.Farmer:

>Your answer lies somewhere in this page
>http://www.python.org/2.2.2/de*scrintro.html


Yes, when it refers to

http://www.python.org/2.3/mro.html

(section Bad Method Resolution Orders).

In short, it is a feature, not a bug.

Michele Simionato

 
Reply With Quote
 
M.E.Farmer
Guest
Posts: n/a
 
      05-02-2005
I knew it was a feature but had only a few minutes to answer and was
playing it safe
You would be surprised how many answers you can squeeze out of that one
URL.
That whole page is worth a month of study( for me anyway ).
I am still trying to grasp the 'purpose' of classmethods.

Michele Simionato wrote:
> > M.E.Farmer:

>
> >Your answer lies somewhere in this page
> >http://www.python.org/2.2.2/de*scrintro.html

>
> Yes, when it refers to
>
> http://www.python.org/2.3/mro.html
>
> (section Bad Method Resolution Orders).
>
> In short, it is a feature, not a bug.
>
> Michele Simionato


 
Reply With Quote
 
Michele Simionato
Guest
Posts: n/a
 
      05-02-2005
John & Sarah:
> Incidentally, is it safe to have an mro() method in a class, > or is

this
> as reserved as the usual __reserved_words__? Does Python > use mro()

(as
> opposed to __mro__) internally or anything?


..mro() is a metamethod, it is a method of the metaclass, not of the
class. So you can override it without problems, and still access it as
type.mro(cls).

For more about metamethods see
http://www-128.ibm.com/developerwork...ta2/index.html

Michele Simionato

 
Reply With Quote
 
Michele Simionato
Guest
Posts: n/a
 
      05-02-2005
M.E. Farmer:

>You would be surprised how many answers you can squeeze out of that

one
>URL.
>That whole page is worth a month of study( for me anyway ).


One month only? You must be pretty smart, one could easily extract
a book from that page

>I am still trying to grasp the 'purpose' of classmethods.


In my personal opinion classmethods and staticmethods could (and
possibly
should) be removed from the language; a part for that consideration,
the typical
use for classmethods is as object factories, to provide alternative
constructors.

Michele Simionato

 
Reply With Quote
 
Michele Simionato
Guest
Posts: n/a
 
      05-02-2005
BTW, what it your use case? I have yet to see a single compelling use
case for multiple inheritance, so I am
curious of what your design is.

Michele Simionato

 
Reply With Quote
 
M.E.Farmer
Guest
Posts: n/a
 
      05-02-2005
Michele Simionato wrote:
> M.E. Farmer:
>
> >You would be surprised how many answers you can squeeze out of that

> one
> >URL.
> >That whole page is worth a month of study( for me anyway ).

>
> One month only? You must be pretty smart, one could easily extract
> a book from that page

No, I was being 'conservative'.
I am not as smart as the fellow that wrote that MRO paper
Honestly, I have had that bookmarked for years...
There are some deep conceps on that page and I have not had a 'use
case' for most of them.
I would gladly welcome the book, you gonna write it!?
Something that covers ( with lots of 'real-life' examples ):
classes ( I see a lot of questions on c.l.py about basic class use )
special methods and class customization ( putting classes to work )
metaclasses ( what they are and when you might need them )
inheritance ( covering all flavors subclassing,mixin,etc.. )
descriptors ( what are they and why you should care )
types ( creating new ones, subclassing old ones, etc... )
old style / newstyle gotchas for classes / metaclasses
and maybe a few pages on decorators.

It is all well and good to have advanced OO in the language,
but it would be better if there was a 'reason' for it all.

> >I am still trying to grasp the 'purpose' of classmethods.

>
> In my personal opinion classmethods and staticmethods could (and
> possibly
> should) be removed from the language; a part for that consideration,
> the typical
> use for classmethods is as object factories, to provide alternative
> constructors.
>
> Michele Simionato


Thank you, I had a feeling that it was just extra fluff.
But I felt/feel that way about list comprehensions and decorators too

M.E.Farmer

 
Reply With Quote
 
Michele Simionato
Guest
Posts: n/a
 
      05-02-2005
Well, writing that book would be a major undertaking that I am not
willing
to take any soon

However, all you are asking for is already in my lectures at ACCU:
http://www.reportlab.org/~andy/accu2...rsofpython.zip

>From the README:


"""
<snip>
Generally speaking these lectures are unpolished, too concise, with
more code than words, and with cutting edge/experimental code.
Read them at your risk and peril. Take in account the fact that they
were prepared as a last minute replacement for Alex Martelli's
tutorial,
with a limited amount of time and a very advanced program to follow.

My main concern in preparing these notes was to give the readers a few
*ideas*, not polished solutions. If you are reading these notes, you
will be more than capable to customize these ideas to your own
situation
and to fix the unavoidable little bugs, imperfections, annoyances.

Whereas I recommend the first lecture about iterators and generators
to everybody, take in account than the second and especially the
third lecture may cause your head to explode. I do not take any
responsability in that case.
"""
Michele Simionato

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need a better understanding on how MRO works? Steven W. Orr Python 6 08-26-2007 03:53 PM
MRO theory Clarence Python 4 04-12-2007 05:32 PM
TypeError: Cannot create a consistent method resolution order (MRO) for bases object digitalorganics@gmail.com Python 8 06-27-2006 06:57 AM
How to devise run-time pluggable classes? (MRO problem) =?iso-8859-1?Q?Fran=E7ois?= Pinard Python 0 08-20-2005 06:35 PM
don't understand MRO Uwe Mayer Python 3 06-24-2005 09:53 AM



Advertisments