Parametrized inheritance

D

Dan Bullok

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

Miki Tebeka

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)
def say(self):
print "woof"


Traceback (most recent call last):
File "<pyshell#75>", line 1, in -toplevel-
a.say()
AttributeError: AlsoWoof instance has no attribute 'say'
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? def f(self):
print "base"

def f(self):
print "sub"

sub

HTH.
Miki
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top