super(obj)?

Martin von Loewis loewis at informatik.hu-berlin.de
Mon Feb 18 11:46:12 EST 2002


Gustavo Niemeyer <niemeyer at conectiva.com> writes:

> AFAICS, this seems to be the most useful version of super:
> 
> super(obj.__class__, obj)

It would usually be wrong. Consider

class Base(object):
  def doit(self):
    pass

class Derived1(Base):
  def doit(self):
    super(Derived1, self).doit()

class Derived2(Derived1):
  def doit(self):
    super(Derived2, self).doit()

obj = Derived2()
obj.doit()

This could does the expected: Derived2.doit call2 Derived1.doit calls
Base.doit. If you change Derived1 to

class Derived1(Base):
  def doit(self):
    super(self.__class__, self).doit()

then self.__class__ would be Derived2, so the Derived1 would call
itself recursively, instead of performing a super call.

HTH,
Martin



More information about the Python-list mailing list