[Python-Dev] locals(), closures, and IronPython...

Guido van Rossum guido at python.org
Mon Mar 5 23:13:39 CET 2007


Jeremy Hylton has been asking questions about this too at the sprint
after PyCon. I'm tempted to accept that the exact behavior of locals()
is implementation-defined (IOW undefined :-) as long as it includes
the locals defined in the current scope; whether it also includes free
variables could be debatable. I don't know too many good use cases for
locals() apart from "learning about the implementation" I think this
might be okay. Though a PEP might be in order to get agreement between
users, developers and other implementation efforts (e.g. PyPy,
Jython).

On 3/5/07, Dino Viehland <dinov at exchange.microsoft.com> wrote:
>
>
>
>
> def a():
>
>                 x = 4
>
>                 y = 2
>
>                 def b():
>
>                                 print y, locals()
>
>                 print locals()
>
>                 b()
>
>
>
> a()
>
>
>
> in CPython prints:
>
>
>
> {'y': 2, 'x': 4, 'b': <function b at 0x020726F0>}
>
> 2 {'y': 2}
>
>
>
> I'm wondering if it's intentional that these don't print dictionaries w/ the
> same contents or if it's more an accident of the implementation.   In other
> words would it be reasonable for IronPython to promote all of the locals of
> a into b's dictionary when both a and b call locals?
>
>
>
> We currently match CPython's behavior here – at least in what we display
> although possibly not in the lifetimes of objects.  We're considering an
> internal change which might alter the behavior here though and end up
> displaying all the members.
>
>
>
>
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> http://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
> http://mail.python.org/mailman/options/python-dev/guido%40python.org
>
>


-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-Dev mailing list