473,569 Members | 2,765 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.h o :
* * * * * * if m == True:
* * * * * * * raise Exception("Inhe ritance 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.Ve ctor))
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,"__mr o__"):
for m in b.__mro__:
if m in seen:
raise Exception("diam ond multiple
inheritance")
seen.add(m)
except Exception, e:
print cls,"has diamond MI"
else:
print cls,"is ok"
Jun 27 '08 #1
2 2553

Sorry I lost the original post.

Paul McGuire <pt***@austin.r r.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.h o :
* * * * * * if m == True:
* * * * * * * raise Exception("Inhe ritance 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("Mult iple 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.Ve ctor))
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,"__mr o__"):
for m in b.__mro__:
if m in seen:
raise Exception("diam ond 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.r r.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,"__mr o__"):
for m in b.__mro__:
if m in seen:
raise Exception("diam ond 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
4323
by: Graham Banks | last post by:
Does using multiple inheritance introduce any more performance overhead than single inheritance?
6
746
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 there a way to list exactly which exceptions that are not handlet? (design time) I could of cause ... use catch (Exception e) {...} as the final...
0
2134
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 one of my ASP.NET 1.1 web forms. This involves a fairly simple inheritance of the System.Web.UI.WebControls.RadioButtonList class, with some new...
0
1243
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 events work everytime. When the container is Internet Explorer, raising the events work on some PCs, but generate the exception: "object does not...
9
1823
by: CuriousGeorge | last post by:
Can someone explain why this code DOES NOT raise a null reference exception? //////////////////////////// Program.cs ///////////////////////////////////////////// using System; using System.Collections.Generic; using System.Text; using CSharpLib;
28
5857
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 exception message. My naive solution was this: try: ... except Exception, e: raise e.__class__, str(e) + ", sorry!"
4
1534
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. It's working pretty well, except that I get the deprecation warning that raising a string exception is going to go away. So my question is, how do...
1
1150
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 h.Vector and h.List are really the same extension class, HocObject. So far I have only been able to do this by iterating over __mro__ during creation...
0
198
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
7697
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8120
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...
0
5219
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
3653
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...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1212
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
937
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.