473,555 Members | 2,297 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 2551

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
1242
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
7547
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
7825
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. ...
1
7588
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
7904
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6176
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
5452
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
3596
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
3575
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2037
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

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.