473,287 Members | 1,492 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.

raising an exception when multiple inheritance involves same base

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 of an object as in:

def hclass(c):
class hc(hoc.HocObject):
def __new__(cls, *args, **kwds):
m = False
for x in cls.__mro__:
if (type(x) == type(hc)):
if m == True:
raise HocError, 'Multiple inheritance...'
m = True
kwds.update({'hocbase':cls.htype})
return hoc.HocObject.__new__(cls, *args, **kwds)
setattr(hc, 'htype', c)
return hc

Is there a way to do the test earlier (e.g during creation of the Foo
class when hclass is called the second time
instead of during creation of a Foo object).
Just before the return hc, print hc.__mro__ yields for the second call:
(<class '__main__.hc'>, <type 'hoc.HocObject'>, <type 'object'>)
wheras in the __new__, print cls.__mro__ yields:
(<class '__main__.Foo'>, <class '__main__.hc'>, <class '__main__.hc'>,
<type 'hoc.HocObject'>, <type 'object'>)
Thanks,
Michael

Jun 27 '08 #1
1 1134
Michael Hines <mi***********@yale.eduwrites:
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 of an object as in:

def hclass(c):
class hc(hoc.HocObject):
def __new__(cls, *args, **kwds):
m = False
for x in cls.__mro__:
if (type(x) == type(hc)):
if m == True:
raise HocError, 'Multiple inheritance...'
m = True
kwds.update({'hocbase':cls.htype})
return hoc.HocObject.__new__(cls, *args, **kwds)
setattr(hc, 'htype', c)
return hc

Is there a way to do the test earlier (e.g during creation of the Foo
class when hclass is called the second time
instead of during creation of a Foo object).
Yes, by giving hoc.HocObject a custom metaclass:

class MetaHocObject(type):
def __new__(cls, name, bases, attrs):
if len(bases) 1:
raise Exception("Only single inheritance allowed")
return type.__new__(cls, name, bases, attrs)

class HocObject(object):
__metaclass__ = MetaHocObject
That'll forbid descendants of MetaHocObject having several bases:
>class A(HocObject): pass
....
>>class B(HocObject): pass
....
>>class C(A, B): pass
....
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in __new__
Exception: Only single inheritance allowed
>>>
HTH

--
Arnaud
Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ian McBride | last post by:
(was: delete() confusion) I have a class with multiple base classes. One of these base classes (base1) has its own new/delete operators and nothing else. Another base class (base 2) has a...
8
by: Shawn Casey | last post by:
Consider the following code: interface IBase { virtual void BaseFunction() = 0; }; interface IDerived : public IBase { virtual void DerivedFunction() = 0;
6
by: Paul | last post by:
In real life situation, do we ever come across a situation where we would need two base objects in an object. A snippet is worth 1000 words (: so... class Base { }; class Derived1:public Base...
9
by: Code4u | last post by:
My colleagues and I have been discussing techniques for implementing interfaces in C++. We're looking for a mechanism similar to COM's QueryInterface, in which a certain types of objects can be...
20
by: km | last post by:
Hi all, In the following code why am i not able to access class A's object attribute - 'a' ? I wishto extent class D with all the attributes of its base classes. how do i do that ? thanks in...
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
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...
11
by: John | last post by:
Hi All, Although C# has Generics, it still does not support the generic programming paradigm. Multiple inheritance is required to support real generic programming. Here is a simple design pattern...
2
by: Paul McGuire | last post by:
On May 25, 8:37 am, Michael Hines <michael.hi...@yale.eduwrote: Here's a more general version of your testing code, to detect *any* diamond multiple inheritance (using your sample classes). --...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
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...

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.