class super method

Steve Holden steve at holdenweb.com
Tue Apr 1 18:18:27 EDT 2008


George Sakkis wrote:
> On Mar 31, 10:41 pm, Ed Leafe <e... at leafe.com> wrote:
> 
>> 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.
> 
> Did you follow the links I gave by any chance? With all the gotchas
> and rules of how to use it properly, it's far from what I would call
> elegant.
> 
>>         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.
> 
> Pehaps, at least as long as you make sure that all superclasses have a
> compatible signature - which in practice typically means accept
> arbitrary *args and **kwargs in every class in the hierarchy like your
> example. Good luck figuring out what's wrong if it's not used
> consistently.
> 
> Also doOurCustomStuffBeforeTheSuperCall() works as long as all
> ancestor methods to be called need the same CustomStuff massaging.
> 
> In a sentence, it's better than nothing but worse than anything.
> 
So you are prepared to write off the voice of experience because some 
random web pages contradict what Ed is saying?

As Ed rightly points out, any sufficiently complex gun can end up 
shooting you in the foot.

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/




More information about the Python-list mailing list