lambda closure question

Carl Banks invalidemail at aerojockey.com
Sun Feb 20 07:39:44 EST 2005


jfj wrote:
> Yes, but according to the python philosophy one could pass locals()
> to the nested function and grab the values from there. Seems
practical
> for the rareness of this...

First of all, I really don't agree that it's a rare need.  I use it
pretty often.

Second of all, the whole point of referencing the outer scope is so you
don't *have* to pass stuff around.  Saying that you can accomplish the
same thing by passing locals() isn't right, because the thing you were
accomplishing was not having to pass anything around.

Say you have a suite of functions, all of which are called by some main
function and each other, and all of which need to access a lot of the
same data.  The best, most straightforward way to do it is to have the
common data be a local variable of the main function, and nest the
suite inside it.  The other way to do it, passing around a common data
structure, add complexity and hurts performance.

And it's a pretty common situation; a lot of recursive algorithms can
make very good use of this.


-- 
CARL BANKS




More information about the Python-list mailing list