Self function

Luis Alberto Zarrabeitia Gomez kyrie at uh.cu
Thu May 7 04:32:23 CEST 2009


Quoting Steven D'Aprano <steven at REMOVE.THIS.cybersource.com.au>:

> 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.

[btw, I took this long before jumping into this thread for this precise
reason... Copy/paste is an evil anti-pattern.

> 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.

A bit offtopic: a while ago I think I saw a recipe for a decorator that, via
bytecode hacks, would bind otherwise global names to the local namespace of the
function. Can anyone remember it/point me to it? An @bind decorator that would
'localize' all the global names, including the still unexistent but already know
function name, would guarantee that at least recursion calls the same function
instead of "whatever happens to be bound to their name at runtime". If it wasn't
a hack, anyway.

-- 
Luis Zarrabeitia
Facultad de Matemática y Computación, UH
http://profesores.matcom.uh.cu/~kyrie


-- 
Participe en Universidad 2010, del 8 al 12 de febrero de 2010
La Habana, Cuba 
http://www.universidad2010.cu




More information about the Python-list mailing list