class super method

George Sakkis george.sakkis at gmail.com
Tue Apr 1 09:43:49 EDT 2008


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.

George



More information about the Python-list mailing list