Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Parametrized inheritance

Reply
Thread Tools

Parametrized inheritance

 
 
Dan Bullok
Guest
Posts: n/a
 
      01-04-2004
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.






 
Reply With Quote
 
 
 
 
Mike C. Fletcher
Guest
Posts: n/a
 
      01-04-2004
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/




 
Reply With Quote
 
 
 
 
Dan Bullok
Guest
Posts: n/a
 
      01-04-2004
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?


 
Reply With Quote
 
Aahz
Guest
Posts: n/a
 
      01-04-2004
In article <sNNJb.741891$Fm2.670283@attbi_s04>,
Dan Bullok <(E-Mail Removed)> 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 ((E-Mail Removed)) <*> 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.
 
Reply With Quote
 
Francis Avila
Guest
Posts: n/a
 
      01-04-2004
#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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Mixing self conscious parametrized types with inheritance SoulSpirit Java 2 03-16-2011 10:46 AM
Mixing self conscious parametrized types with inheritance Robert Klemme Java 8 03-15-2011 01:16 PM
Update Using a parametrized Query jagdishl@gmail.com ASP .Net 1 11-08-2005 09:08 PM
opening parametrized window in asp.net without post back-Help required! Keshav Gadia ASP .Net 2 09-13-2004 10:05 AM
Parametrized inheritance Dan Bullok Python 1 01-04-2004 08:44 AM



Advertisments