class super method

Ed Leafe ed at leafe.com
Mon Mar 31 22:41:37 EDT 2008


On Mar 31, 2008, at 5:58 PM, George Sakkis wrote:

>> is there any tutorial for super method (when/how to use it)?
>>
>> or maybe someone could explain me how it works?
>>
>> thx
>
> Super is one of the dark corners of the language [1,2]... a good rule
> of thumb is to stay away from it, or at least stick to its basic
> usage.


	I disagree - super is quite elegant and dependable.

	Because Python support multiple inheritance, it is difficult to  
manually ensure that when augmenting a method that the correct  
superclass calls are made. super() handles that without having to  
guess as to what the correct inheritance hierarchy is.

	In my own project (Dabo), we use mixin classes liberally to provide  
consistent behavior across our UI classes. The use of super makes  
customizing __init__() behavior, for example, quite straightforward.  
The general form looks like:

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

	This has worked reliably for us in every place where we have used it.  
There's nothing dark and mysterious about it at all.

-- Ed Leafe






More information about the Python-list mailing list