By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,235 Members | 1,022 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,235 IT Pros & Developers. It's quick & easy.

Re: raising an exception when multiple inheritance involves samebaseThank

P: n/a
On May 25, 8:37*am, Michael Hines <michael.hi...@yale.eduwrote:
Thanks very much, Arnaud. That is exactly the hint I needed. Since it is
not multiple inheritance per se I prohibit but only multiple inheritance
involving more than one HocObject class, I replaced your len(bases) 1
test with
<code>
* * m = False
* * for b in bases :
* * * if hasattr(b, '__mro__'):
* * * * for bb in b.__mro__ :
* * * * * if bb == MetaHocObject.ho :
* * * * * * if m == True:
* * * * * * * raise Exception("Inheritance of multiple HocObject not
allowed")
* * * * * * m = True

</code>
to get

class A(HocObject): pass

class B(object): pass

class C(): pass

class D(C, B, HocObject): pass # ok

class D(C, A, HocObject): pass # fail

When I fold this idea into my code I may even try to eliminate the class
factory aspect of
class Foo(hclass(h.Vector))
in favor of
class Foo(h.Vector)

Thanks again,
Michael
Here's a more general version of your testing code, to detect *any*
diamond multiple inheritance (using your sample classes).

-- Paul
for cls in (A,B,C,D):
seen = set()
try:
bases = cls.__bases__
for b in bases:
if hasattr(b,"__mro__"):
for m in b.__mro__:
if m in seen:
raise Exception("diamond multiple
inheritance")
seen.add(m)
except Exception, e:
print cls,"has diamond MI"
else:
print cls,"is ok"
Jun 27 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a

Sorry I lost the original post.

Paul McGuire <pt***@austin.rr.comwrites:
On May 25, 8:37*am, Michael Hines <michael.hi...@yale.eduwrote:
>Thanks very much, Arnaud. That is exactly the hint I needed. Since it is
not multiple inheritance per se I prohibit but only multiple inheritance
involving more than one HocObject class, I replaced your len(bases) 1
test with
<code>
* * m = False
* * for b in bases :
* * * if hasattr(b, '__mro__'):
* * * * for bb in b.__mro__ :
* * * * * if bb == MetaHocObject.ho :
* * * * * * if m == True:
* * * * * * * raise Exception("Inheritance of multiple HocObject not
allowed")
* * * * * * m = True

</code>
I think you don't need to look at the bases' mros, just use
issubclass(), e.g. (untested):

if sum(1 for b in bases if issubclass(b, HocObject)) 1:
raise Exception("Multiple inheritance from HocObject")
>to get

class A(HocObject): pass

class B(object): pass

class C(): pass

class D(C, B, HocObject): pass # ok

class D(C, A, HocObject): pass # fail

When I fold this idea into my code I may even try to eliminate the class
factory aspect of
class Foo(hclass(h.Vector))
in favor of
class Foo(h.Vector)

Thanks again,
Michael

Here's a more general version of your testing code, to detect *any*
diamond multiple inheritance (using your sample classes).

-- Paul
for cls in (A,B,C,D):
seen = set()
try:
bases = cls.__bases__
for b in bases:
if hasattr(b,"__mro__"):
for m in b.__mro__:
if m in seen:
raise Exception("diamond multiple
inheritance")
seen.add(m)
except Exception, e:
print cls,"has diamond MI"
else:
print cls,"is ok"
All new-style classes descend from object, so any new-style class
with at least two bases makes a diamond! For example the first
version of class D above inherits twice from object, so it will be
caught.

OTOH, old-style classes don't have an __mro__, so this will not catch
diamond inheritance of old-style classes. E.g

class A: pass
class B(A): pass
class C(A): pass
class D(B, C): pass

--
Arnaud
Jun 27 '08 #2

P: n/a
En Sun, 25 May 2008 13:32:39 -0300, Paul McGuire <pt***@austin.rr.comescribió:
Here's a more general version of your testing code, to detect *any*
diamond multiple inheritance (using your sample classes).

for cls in (A,B,C,D):
seen = set()
try:
bases = cls.__bases__
for b in bases:
if hasattr(b,"__mro__"):
for m in b.__mro__:
if m in seen:
raise Exception("diamond multiple
inheritance")
seen.add(m)
except Exception, e:
print cls,"has diamond MI"
else:
print cls,"is ok"
I think you should exclude the `object` class from the test, because all new-style classes inherit from it and *every* case of multiple inheritance forms a diamond.

--
Gabriel Genellina

Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.