Backward chaining for __init__?

Neel Krishnaswami neelk at brick.cswv.com
Tue May 2 22:50:29 EDT 2000


Courageous <jkraska1 at san.rr.com> wrote:
> 
> I recently wrote a test program with a Parent class and a Derived
> class, each one implementing __init__.  To my surprise and chagrine,
> only the Child's __init__ method was called. Worse, I can't figure
> out a way to call the method (or methods, in the case of multiple
> inheritance) in the Parent.
> 
> How does one go about implementing backward chaining for
> constructor's in Python?

Like so:

  class Foo:
      def __init__(self):
  	  self.a = "every man is evil, yes, every man's a liar"

  class Bar(Foo):
      def __init__(self):
  	  Foo.__init__(self)
  	  self.b = "and unashamed with a wicked tongue"

  class Baz(Bar):
      def __init__(self):
  	  Bar.__init__(self)
  	  self.c = "he sings in the black soul choir"

Which produces:

  >>> q = Baz()
  >>> q.a
  "every man is evil, yes, every man's a liar"
  >>> q.b
  'and unashamed with a wicked tongue'
  >>> q.c
  'he sings in the black soul choir'

Admittedly, this is kind of a let-down if you're used to Common Lisp's
baroquely overengineered method modifers :), but it works.


Neel



More information about the Python-list mailing list