Backward chaining for __init__?

Mike Fletcher mfletch at tpresence.com
Tue May 2 22:43:20 EDT 2000


It's hackish, but something like:

def doParentInits( self, args, namedargs ):
	args = (self,)+args
	for klass in self.__class__.__bases__:
		if hasattr( klass, '__init__'):
			apply( klass.__init__, args, namedargs )

Would work, assuming that args and namedargs were compatible with all the
base classes.  In the real world, you normally wind up doing:

class a:
	def __init__( self, d, e):
		whatever()
class x( a):
	def __init__( self, b,c,d, e, *args, **namedargs):
		self.b = b
		self.c = c
		a.__init__( self, d, e )
		# or
		apply( a.__init__, (self,)+ args, namedargs )

Ugly, yes, but best I know of off the top of my head. (1.6 makes the syntax
a little less awkward by allowing *args, **namedargs values in the call of
methods/functions).

HTH,
Mike


-----Original Message-----
From: Courageous [mailto:jkraska1 at san.rr.com]
Sent: Tuesday, May 02, 2000 10:16 PM
To: python-list at python.org
Subject: Backward chaining for __init__?



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?



C/
-- 
http://www.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list