473,324 Members | 2,567 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,324 software developers and data experts.

Parametrized inheritance

I have a couple of classes:
class Base:
...

class Sub(Base):
...
I want to subclass Base and Sub, i.e. define classes:
class MyBase(Base):
...

class MySub(Sub):
...

The inheritance looks like this:
Base->MyBase
->Sub->MySub

But I'd really like it to look like this:
Base->MyBase->Sub->MySub

i.e. define Sub as "class Sub(X)", where I can change X at the time of
instantiation. Then I could define MySub as "class MySub(Sub(MyBase))".
(I hope that it's obvious that I'm looking for this effect, not this syntax)
Of course, someone is going to ask "why?"

I need to override a few members in Base. Sub is mostly fine as-is, so if I
could have MySub=Sub(MyMBase), that would be fine.


Jul 18 '05 #1
4 1451
Dan Bullok wrote:
....
The inheritance looks like this:
Base->MyBase
->Sub->MySub

But I'd really like it to look like this:
Base->MyBase->Sub->MySub

i.e. define Sub as "class Sub(X)", where I can change X at the time of
instantiation. Then I could define MySub as "class MySub(Sub(MyBase))".
(I hope that it's obvious that I'm looking for this effect, not this syntax)

Works quite nicely in Python 2.2 with only one minor change. In particular use of object as base for Base:
class Base(object): .... pass
.... class Sub( object ): .... pass
.... class MyBase( Base ): .... pass
.... class MySub( Sub, MyBase ): .... pass
.... MySub.mro() [<class '__main__.MySub'>, <class '__main__.Sub'>,
<class '__main__.MyBase'>, <class '__main__.Base'>,
<type 'object'>]


You can use the super( ... ) built-in to provide really elegant support
for mix-in classes overriding base-class operations cooperatively.

BTW, you could have made Sub a sub-class of Base in the example above
and *still* had it give you the desired method-resolution-order, (a nice feature of Python's multiple-inheritance mechanism, IMO). (I made Sub's parent object to make it clear how the inheritance works.)

You can find more information about these kinds of patterns by searching for "Mix-In Class" and/or "Multiple Inheritance". Python 2.2 changed how multiple inheritance graphs are constructed in Python, BTW. If you used old-style classes the mro would have been [MySub, Sub, Base, MyBase, Base] IIRC.

HTH,
Mike

_______________________________________
Mike C. Fletcher
Designer, VR Plumber, Coder
http://members.rogers.com/mcfletch/


Jul 18 '05 #2
Mike C. Fletcher wrote:
You can find more information about these kinds of patterns by searching
for "Mix-In Class" and/or "Multiple Inheritance". Python 2.2 changed how
multiple inheritance graphs are constructed in Python, BTW. If you used
old-style classes the mro would have been [MySub, Sub, Base, MyBase, Base]
IIRC.


Well, I thought of doing this, but after looking up resolution order (sec
9.5.1 of the tutorial) I found that resolution is done "depth-first,
left-to-right", In my example, that would have given [MySub, Sub, Base,
MyBase, Base], but, I checked, using mro(), and found that it was indeed
[MySub, Sub, MyBase, Base], just as I needed it. (thanks for the mro() tip
- didn't know about that one). Unless I've gone crosseyed (possible, it's
late), this is NOT depth-first, left-to-right. So is the tutorial out of
date?
Jul 18 '05 #3
In article <sNNJb.741891$Fm2.670283@attbi_s04>,
Dan Bullok <my*********@mylastname.com> wrote:
Mike C. Fletcher wrote:

You can find more information about these kinds of patterns by searching
for "Mix-In Class" and/or "Multiple Inheritance". Python 2.2 changed how
multiple inheritance graphs are constructed in Python, BTW. If you used
old-style classes the mro would have been [MySub, Sub, Base, MyBase, Base]
IIRC.


Well, I thought of doing this, but after looking up resolution order (sec
9.5.1 of the tutorial) I found that resolution is done "depth-first,
left-to-right", In my example, that would have given [MySub, Sub, Base,
MyBase, Base], but, I checked, using mro(), and found that it was indeed
[MySub, Sub, MyBase, Base], just as I needed it. (thanks for the mro() tip
- didn't know about that one). Unless I've gone crosseyed (possible, it's
late), this is NOT depth-first, left-to-right. So is the tutorial out of
date?


As Mike said, it's different for new-style classes. See
http://www.python.org/2.2.3/descrintro.html
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

Weinberg's Second Law: If builders built buildings the way programmers wrote
programs, then the first woodpecker that came along would destroy civilization.
Jul 18 '05 #4
#quote#
Dan Bullok wrote in message ...
Mike C. Fletcher wrote:
You can find more information about these kinds of patterns by searching
for "Mix-In Class" and/or "Multiple Inheritance". Python 2.2 changed how
multiple inheritance graphs are constructed in Python, BTW. If you used
old-style classes the mro would have been [MySub, Sub, Base, MyBase, Base]
IIRC.


Well, I thought of doing this, but after looking up resolution order (sec
9.5.1 of the tutorial) I found that resolution is done "depth-first,
left-to-right", In my example, that would have given [MySub, Sub, Base,
MyBase, Base], but, I checked, using mro(), and found that it was indeed
[MySub, Sub, MyBase, Base], just as I needed it. (thanks for the mro() tip
- didn't know about that one). Unless I've gone crosseyed (possible, it's
late), this is NOT depth-first, left-to-right. So is the tutorial out of
date?
#endquote#

I just looked and, yes indeed, it is out of date (file a bug report).
Python 2.2 used this mro, which was changed in 2.3. 2.3 uses a so-called
'C3' mro, which is a bit more complex. See
http://www.python.org/2.3/mro.html.

The best documentation on Python classes WRT the new-style changes is an
essay by Guido at http://www.python.org/2.2.2/descrintro.html. It says 2.2,
but it mentions 2.3 changes as inline addendums.

Odd thing is, filename is "descriptor intro" yet it doesn't mention
descriptors. There's a good reference on the madness that is descriptors at
http://users.rcn.com/python/download/Descriptor.htm.
--
Francis Avila

Jul 18 '05 #5

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

Similar topics

2
by: AIM | last post by:
Error in msvc in building inheritance.obj to build hello.pyd Hello, I am trying to build the boost 1.31.0 sample extension hello.cpp. I can not compile the file inheritance.cpp because the two...
3
by: rwawryk | last post by:
Hi, Does anybody know how to implement parametrized stream operator (such as setw, setfill)? I need to put into the stream variable of type char* without terminating NULL. It would be great if I...
1
by: Ilpo Nyyssönen | last post by:
Take an XML source: <document> <recipient> <name>John Doe</name> <email>john.doe@example.invalid</email> </recipient> <recipient> <name>Jane Doe</name>...
22
by: nobody | last post by:
hello everybody, is there a way of creating an array with help of a function that would accept the name of this array as a parameter and then create global Array type variable of that name? so...
4
by: JKop | last post by:
I'm starting to think that whenever you derive one class from another, that you should use virtual inheritance *all* the time, unless you have an explicit reason not to. I'm even thinking that...
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
4
by: mario.rossi | last post by:
Hi all, I am trying to invoke the default constructor from another, parametrized, constructor, but the default constructor doesn't get invoked at all, I saw. Is this correct ISO C++ behaviour or...
0
by: Halimaji Nijazi | last post by:
Hi everybody I've read a lot about parametrized services and now I want to test it. My problem is, how should I create a simple parametrized service? How starting this service whithin .NET? ...
2
by: weird0 | last post by:
Hi! On the recommendation of one of the MVP's on this group....... I tried writing parametrized queries. But the fucking thing does not work and it does not update the data in the table. I...
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: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.