class super method

Ed Leafe ed at leafe.com
Tue Apr 1 09:31:26 EDT 2008


On Apr 1, 2008, at 7:23 AM,  
brooklineTom at gmail_spam_blocking_suffix.com wrote:

> I've also found myself wondering:
>
> 1. What are the two arguments to super used for?

	To determine the type (1st arg) for which the superclass hierarchy is  
to be determined, and the instance (2nd arg) if the super object is to  
be bound. In typical usage, they are the class of the current  
instance, and the 'self' reference to the instance.

> 2. What happens when the method supplied *after* the super is
> different from the containing method?
>
> In the above example what happens if:
>
> class DaboUIClass(dabo.ui.dControlMixin, someWxPythonClass):
> 	def __init__(self, *args, **kwargs):
> 		doOurCustomStuffBeforeTheSuperCall()
> 		super(DaboUIClass, self).callRandomMethod(foobar)
> 		doOurCustomStuffAfterTheSuperCall()


	super() returns an object that doesn't "know" in what method it was  
derived, so as long as the superclass has the 'callRandomMethod'  
method, and 'foobar' is defined, there is no problem at all. IOW, the  
only context is the class of the instance, not any particular method  
of the instance.

	You could also do something like this:

class DaboUIClass(dabo.ui.dControlMixin, someWxPythonClass):
	def __init__(self, *args, **kwargs):
		doOurCustomStuffBeforeTheSuperCall()
		self.super = super(DaboUIClass, self)
		self.super.__init__(*args, **kwargs)
		doOurCustomStuffAfterTheSuperCall()

	def someMethod(self, foo, bar):
		self.super.someMethod(foo, bar)

	def someOtherMethod(self):
		self.super.someOtherMethod()

-- Ed Leafe






More information about the Python-list mailing list