class super method

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Apr 1 13:05:09 EDT 2008


En Tue, 01 Apr 2008 11:21:48 -0300, Ed Leafe <ed at leafe.com> escribió:
> On Apr 1, 2008, at 8:43 AM, George Sakkis wrote:
>
>> 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.
>
> 	See my comment above. If you do not know what you're doing, you
> shouldn't be doing it. This is not the fault of super(); it's the
> fault of a poor programmer. And I used generic *args and **kwargs in
> the method sig since I was using made-up class names and methods.
> Would you have reacted more favorably if I had used (self, foo, bar)
> instead?

Then *all* classes must use exactly the same signature for that method.  
You don't know which one will be the next class in the MRO order used by  
super() so you can't adjust the arguments in any way.
In the case of __init__, the only practical way is to make all of them  
take keyword arguments exclusively and use *args and **kwarsg.
See the earlier links for a simple failing example.

>> Also doOurCustomStuffBeforeTheSuperCall() works as long as all
>> ancestor methods to be called need the same CustomStuff massaging.
>
> 	Oh, c'mon. Of course that's the case; if you are overriding method
> behavior, it is your job as the programmer to ensure that. Again, this
> is nothing to do with the super() function, and everything to do with
> the abilities of the developer.

How do you know that? super may call a method on a different class that is  
*not* an ancestor of the current class. Again, for examples, see the  
earlier links.

>> In a sentence, it's better than nothing but worse than anything.
> 	I guess I must be the world's most amazing Python developer, as I've
> used super() extensively for years without ever suffering any of the
> pitfalls you and others describe.

Maybe you're a very lucky man!

-- 
Gabriel Genellina




More information about the Python-list mailing list