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

    Miki Tebeka Guest

    Hello Dan,

    > 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)
    >>> class Woof:

    def say(self):
    print "woof"


    >>> class AlsoWoof:

    pass

    >>> a = AlsoWoof()
    >>> a.say()


    Traceback (most recent call last):
    File "<pyshell#75>", line 1, in -toplevel-
    a.say()
    AttributeError: AlsoWoof instance has no attribute 'say'
    >>> AlsoWoof.__bases__ += (Woof,)
    >>> a = AlsoWoof()
    >>> a.say()

    woof
    >>>


    > 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.

    Why don't just override them in the subclass as usuall?
    >>> class Base:

    def f(self):
    print "base"


    >>> class Sub(Base):

    def f(self):
    print "sub"


    >>> s = Sub()
    >>> s.f()

    sub

    HTH.
    Miki
    Miki Tebeka, Jan 4, 2004
    #2
    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:
    627
    Keshav Gadia
    Sep 13, 2004
  2. Replies:
    1
    Views:
    1,606
    S. Justin Gengo
    Nov 8, 2005
  3. Dan Bullok

    Parametrized inheritance

    Dan Bullok, Jan 4, 2004, in forum: Python
    Replies:
    4
    Views:
    349
    Francis Avila
    Jan 4, 2004
  4. Robert Klemme
    Replies:
    8
    Views:
    276
    Robert Klemme
    Mar 15, 2011
  5. SoulSpirit
    Replies:
    2
    Views:
    278
    Robert Klemme
    Mar 16, 2011
Loading...

Share This Page