help me

Alex Martelli aleaxit at yahoo.com
Mon Oct 11 03:24:44 EDT 2004


Andrew Dalke <adalke at mindspring.com> wrote:

> dataangel wrote:
> > Well, I remember looking up the exec keyword and it saying executes a
> > string. Thus, it seemed like the normal way to execute a string, lol.
> 
> But why did that seem like it should be the normal way
> to do variable assignment to global?
> 
> BTW, locals() doesn't work like you want in part because
> that prevents a performance optimization where local
> variable lookups are done in O(1) time as an offset
> into a fixed size table.  I suppose it would be possible
> to have locals() be able to manipulate that table behind
> the scenes, but then that would be complicated and complex.

Very -- and for no good purpose, because just about every beginner's
desire to "assign variables with names determined at runtime", once
analyzed in a bit more depth, turns out to be susceptible of some far
preferable approach (9 out of 10 involving some dict...).

BTW, any function incuding an 'exec' statement has the optimization you
mentioned turned off -- thus the whole function runs dog-slow...


Alex



More information about the Python-list mailing list