473,396 Members | 1,989 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Re: raising an exception when multiple inheritance involves samebaseThank

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
2 2517

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Graham Banks | last post by:
Does using multiple inheritance introduce any more performance overhead than single inheritance?
6
by: Jesper Ordrup Christensen | last post by:
Say I've created a piece of code that involves both sql, io and some number conversions. Being a responsible developer I have tried to catch all of the exceptions - but how can I be sure? Is...
0
by: webmaster | last post by:
Hi all, I'm tearing my hair out with this one. I have successfully implemented by own RadioButtonList in order to provide additional functionality and a DIV rather than TABLE-based layout in...
0
by: Apu Nahasapeemapetilon | last post by:
Suggestions on cross-posting this issue would be appreciated. I've got a C# .NET DLL (CLR v1.1) that raises events into its container. When the container is a .NET application, raising the...
9
by: CuriousGeorge | last post by:
Can someone explain why this code DOES NOT raise a null reference exception? //////////////////////////// Program.cs ///////////////////////////////////////////// using System; using...
28
by: Christoph Zwerschke | last post by:
What is the best way to re-raise any exception with a message supplemented with additional information (e.g. line number in a template)? Let's say for simplicity I just want to add "sorry" to every...
4
by: Silfheed | last post by:
Heyas So this probably highlights my lack of understanding of how naming works in python, but I'm currently using FailUnlessRaises in a unit test and raising exceptions with a string exception. ...
1
by: Michael Hines | last post by:
Hello, I have a class factory that supports single inheritance but it is an error if the base appears twice. e.g class Foo(hclass(h.Vector), hclass(h.List)): should raise an exception since...
0
by: Scott David Daniels | last post by:
Here are some tweaks on both bits of code: Paul McGuire wrote: .... m = for b in bases: if hasattr(b, '__mro__'): if MetaHocObject.ho in b.__mro__: m.append(b) if m:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
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...

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.