Parametrized inheritance

Discussion in 'Python' started by Dan Bullok, Jan 4, 2004.

  1. Dan Bullok

    Dan Bullok Guest

    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.
    Dan Bullok, Jan 4, 2004
    #1
    1. Advertising

  2. 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/
    Mike C. Fletcher, Jan 4, 2004
    #2
    1. Advertising

  3. Dan Bullok

    Dan Bullok Guest

    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?
    Dan Bullok, Jan 4, 2004
    #3
  4. Dan Bullok

    Aahz Guest

    In article <sNNJb.741891$Fm2.670283@attbi_s04>,
    Dan Bullok <> 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 () <*> 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.
    Aahz, Jan 4, 2004
    #4
  5. #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
    Francis Avila, Jan 4, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Keshav Gadia
    Replies:
    2
    Views:
    619
    Keshav Gadia
    Sep 13, 2004
  2. Replies:
    1
    Views:
    1,599
    S. Justin Gengo
    Nov 8, 2005
  3. Dan Bullok

    Parametrized inheritance

    Dan Bullok, Jan 4, 2004, in forum: Python
    Replies:
    1
    Views:
    295
    Miki Tebeka
    Jan 4, 2004
  4. Robert Klemme
    Replies:
    8
    Views:
    268
    Robert Klemme
    Mar 15, 2011
  5. SoulSpirit
    Replies:
    2
    Views:
    272
    Robert Klemme
    Mar 16, 2011
Loading...

Share This Page