By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,706 Members | 1,105 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,706 IT Pros & Developers. It's quick & easy.

Parametrized inheritance

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
#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 discussion thread is closed

Replies have been disabled for this discussion.