473,568 Members | 2,795 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

MRO problems with diamond inheritance?

Trying to create the "lopsided diamond" inheritance below:
class B(object):pass
class D1(B):pass
class D2(D1):pass
class D(D1, D2):pass 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):pass
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 misunderstandin g) 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_word s__? Does Python use mro() (as
opposed to __mro__) internally or anything?

Thanks

John

Jul 19 '05 #1
13 3268
Your answer lies somewhere in this page ;)
http://www.python.org/2.2.2/descrintro.html
M.E.Farmer

Jul 19 '05 #2
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-
Jul 19 '05 #3
> 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

Jul 19 '05 #4
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


Jul 19 '05 #5
John & Sarah:
Incidentally, is it safe to have an mro() method in a class, > or is this as reserved as the usual __reserved_word s__? 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

Jul 19 '05 #6
M.E. Farmer:
You would be surprised how many answers you can squeeze out of that oneURL.
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

Jul 19 '05 #7
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

Jul 19 '05 #8
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,mix in,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

Jul 19 '05 #9
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

Jul 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
2360
by: Pedro | last post by:
Hi, all. I'm having a problem which vb developers had for certain al least once. I have my base form, declared as musthinherit; Public MustInherit Class frmsdvPrincipal
3
1712
by: Tom | last post by:
Hello, I've searched groups and the std unsuccessfully for an explanation of the following - I'd appreciate any comments you may have. Given the following diamond multiple inheritance pattern, with the base class having 2 constructors, only the default constructor is called. Is there a way to require the other constructor to be used? ...
3
2491
by: Tony Johansson | last post by:
Hello Experts!! Assume you have the following diamond inheritance structure. Assume the following at the top you have the Ancestor class and below this class at the same level we have class Child1 and Child2. Class Descendatnt is derived class Child1 and Child2. Assume also that in the top class we have one datamember called number and...
4
2745
by: Alex Hunsley | last post by:
I've seen a few discussion about the use of 'super' in Python, including the opinion that 'super' should only be used to solve inheritance diamond problem. (And that a constructor that wants to call the superclass methods should just call them by name and forget about super.) What is people's opinion on this? Does it make any sense?
2
3922
by: Darryn Ross | last post by:
Hi.. I have a standard windows app in C#. The project contains a two forms and one class that i have created. I want both of the forms to inherit from the class but i am getting an error reading "type in interface list is not an interface"??? i am not sure how to fix the problem. Is an interface (form) unable to inherit from a non interface...
4
2050
by: Schüle Daniel | last post by:
Hello C++ NG, I encountered a strange behaviour in this code #include <iostream> #include <cstdlib> #include <string> using std::cout; using std::endl;
0
7916
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8117
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7660
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6275
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5498
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
932
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.