Parametrized inheritance

Miki Tebeka miki.tebeka at zoran.com
Sun Jan 4 03:44:39 EST 2004


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



More information about the Python-list mailing list