using super

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Tue Jan 1 05:32:06 EST 2008


On Mon, 31 Dec 2007 22:56:41 -0800, iu2 wrote:

> Indeed I might want to chain methods in all sort of ways:
> @chain_call_before - call the parent's method before the derived method
> @chain_call_after - call the parent's method after the derived method
> @chain_call_sum - sum the result of the parent's method with the result
> of the derived method
> @chain_call_list - make a list from the result of the parent's method
> and the result of the derived method

Indeed there are all sorts of ways you might want to call the parent 
class' method. How about a 

@chain_call__multiply_by_seven_and_append_to_list

decorator?


[snip]

>>simply remind people to call
>> A.foo(self, <args>) within the definition of foo in B.
> 
> Sorry, I can't agree to this (although there is nothing else I can do.. 
> . Reminding is not "simply" at all. Why REMIND people do stuff and not
> let Python handle it automatically? 

But Python can't handle it automatically, not unless you do

import read_my_mind_and_do_what_I_want

first. (Module expected in Python 9.7, due out in 2058.)

Since you have to call the superclass yourself, there's not that much 
difference between reminding people to call the superclass by hand, and 
reminding them to call some decorator.

Admittedly, a decorator for the most common cases would be easier to use 
than super(), but I don't think it would make enough of a difference that 
it is worth changing Python's object model just to make it possible. As 
the Zen of Python says:

"Special cases aren't special enough to break the rules."

(or in this case, to change the rules).

And as others have pointed out, if you really want this desperately 
enough, you can create a metaclass to handle it.



-- 
Steven



More information about the Python-list mailing list