class super method
brooklineTom at gmail_spam_blocking_suffix.com
brooklineTom at gmail_spam_blocking_suffix.com
Tue Apr 1 08:23:18 EDT 2008
On Mon, 31 Mar 2008 21:41:37 -0500, Ed Leafe <ed 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.
>
> 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.
Perhaps it's the oo-think that seems "dark and mysterious", rather
than anything about super. In my experience, "super" works just fine.
It is a bit tedious, in that it's all too easy to forget to change the
first parameter when moving a method that contains super to a
different class.
I've also found myself wondering:
1. What are the two arguments to super used for?
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()
More information about the Python-list
mailing list