473,287 Members | 1,709 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,287 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 2505

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: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.