[Python-Dev] nonlocals() function?

average dreamingforward at gmail.com
Mon Apr 5 01:03:17 CEST 2010


On Sat, Apr 3, 2010 at 6:31 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:
> Steve Bonner wrote:
>> What do we think of adding a built-in nonlocals() function that would
>> be similar to globals() and locals()?  Like those functions, it would
>> return a dictionary of variable names and their values. Since we now
>> have the nonlocal statement, it would be consistent to keep the
>> three scopes local/nonlocal/global with parallel capabilities. And it
>> might sometimes be useful for code inside a nested function to see
>> what variables are available at the enclosing level.
>
> That isn't as easy as it may sound. locals() and globals() are each
> single namespaces, while the nonlocals may actually span multiple
> namespaces.

This is probably is way off, but why not use sets.  It seems that
these special "functions" (which actually don't do any
transformations) should be properties or attributes, and in any case
would be better served by returning (holding) sets, then one could do
standard set operations on them (including the set difference
operations upon the set named "locals").

These old functions predate the introductions of attributes/properties
and sets, and like anything that is sure to be a group containing no
duplicates, it should really return a set to remove the ambiguity to
the programmer (unlike returning a list as is presently).  That
includes dir() too and probably others.

python3k-to-you-toly yours,

marcos


More information about the Python-Dev mailing list