Self function

Steven D'Aprano steven at
Thu May 7 03:48:28 CEST 2009

On Thu, 07 May 2009 01:59:02 +0100, Rhodri James wrote:

> On Wed, 06 May 2009 23:33:20 +0100, Luis Alberto Zarrabeitia Gomez
> <kyrie at> wrote:
>> Quoting Rhodri James <rhodri at>:
>>> So the answer to my question would be "nothing"?
>> Indeed, there is nothing broken with the search and replace feature of
>> his editor. When he is copying a non-recursive function, it is
>> _useless_ to do a search and replace. When he is copying a recursive
>> function, it is _required_ to do a search and replace.
> On the contrary, it's merely not very useful for a non-recursive
> function.

Whether search and replace is "not very useful" or "not useful at all", 
it's still an inconvenience and a PITA. Calling up S&R to search through 
a six line function is ridiculously overkill, but if you don't do it, 
you'll surely be bitten.

But regardless, everyone is missing the most important point: why are you 
copying and pasting code in the first place? That is surely very close to 
the top of the list of Worst Ever Anti-Patterns, and it should be avoided 
whenever possible.

In Python, we can avoid much copy-and-paste coding with decorators. Given 
one function, we can create as many variants as we want, differing in pre-
processing of arguments and post-processing of results, by using 
decorators. This is a powerful ability to have, but it's crippled for 
many recursive functions, because recursive functions in Python don't 
actually call themselves, they call whatever happens to be bound to their 
name at runtime.

As Luis said, all(?) programming languages have this same weakness. Just 
because it's a weakness shared by all languages, doesn't stop it from 
being a weakness. My proposal will lesson (but not entirely eliminate) 
this weakness.


More information about the Python-list mailing list