473,770 Members | 1,953 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(MyBas e))".
(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(MyMBa se), that would be fine.


Jul 18 '05 #1
4 1466
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
instantiatio n. Then I could define MySub as "class MySub(Sub(MyBas e))".
(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__.MyBas e'>, <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$F m2.670283@attbi _s04>,
Dan Bullok <my*********@my lastname.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**@pythoncra ft.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
4380
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 files containing some templates: adjacency_list.hpp and mem_fn.hpp can not compile. Does anyone have any solutions?
3
3346
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 had the manipulator which allows to determine maximum number of characters ( let's assume maxw( char* str, int maxlen ) ). example: cout << maxw("Crocodile", 5 );
1
1308
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> <email>jane.doe@example.invalid</email>
22
2762
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 that for example the following code would work as well in browsers as under Windows Scripting Host: str = "tableA";
4
2901
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 there shouldn't have been a "virtual" keyword for this purpose, but instead, a "nonvirtual" keyword! In teaching inheritance, you see the common example: class Vehicle {}
22
23384
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 examples?
4
1742
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 my compiler has a ++bug (overflowing)? struct Test { int x; int y; // Test() {
0
1024
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? Do someone has samples or sample code or any step-by-step instructions? Thanks alot
2
1751
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 gotta do my work by concatenation right now. But what is wrong with the code anyway? Can anyone figure out.
0
9617
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10099
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10036
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9904
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8929
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7451
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5354
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4007
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
3
2849
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.