473,399 Members | 2,278 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,399 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 1136
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
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...
0
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,...
0
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...

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.