2010/4/6 Antoine Pitrou <solipsis@pitrou.net>
Greg Ewing <greg.ewing <at> canterbury.ac.nz> writes:
Maybe it would be better to deprecate globals() and locals() and replace them with another function called something like scope().
It is useful to distinguish between globals (i.e., module-level variables) and locals, so replacing them with scope() would not be better IMO.
It would return a mapping object that looks up names in the current scope. It could also improve on locals() by being writable.
If you can prove that making locals() (or its replacement) writable doesn't complicate the interpreter core too much, then why not. Otherwise -1 :-)
Regards
Antoine.
It will certainly. There's MUCH that can be optimized to let CPython squeeze more performance from static analysis (even a gross one) on locals. Example: def f(): a = 1 b = 2 return a + b can be reduced to something similar to: def f(): a = 1 b = 2 return 3 and, more aggressively, like: def f(): return 3 They are just "dummy" examples, but can make it clear how far optimizations can go with static analysis on locals. Python is a language that make it possible to use such analysis at compile time, and I think it is a very good thing. Obviously the last example brings questions regards the language semantic: is it right to suppress "unused" or "not useful" local variables? A "conservative" answer will be clearly NO. But I hope that a future language specification will fix some aspects, putting clear what you can expect from the language itself, and what is closet to the implementation. Cesare