[issue9274] code.InteractiveInterpreter fails to change locals when invoked in a function

R. David Murray report at bugs.python.org
Sat Jul 17 01:08:46 CEST 2010


R. David Murray <rdmurray at bitdance.com> added the comment:

locals() does not give you a copy of the locals dictionary that you can modify and expect the values to affect the actual locals they were copied from.  This is documented:

http://docs.python.org/library/functions.html#locals

You would need to pass InteractiveInterpreter the "real" locals dict from the frame to make your code work, and it sounds like you've already done this.  However, I don't believe that this is going to work in the general case (eg: in the face of nested scopes), which is why locals() is not updatable. The use case for InteractiveInterpreter is implementing a python-command-line-like utility, and thus has no need to update the locals in the current function.

A doc note about this in InteractiveInterpreter along the lines of the one in the 'exec' docs is almost certainly appropriate, so I'll leave this open as a doc bug.

----------
assignee:  -> docs at python
components: +Documentation -Interpreter Core
nosy: +docs at python
stage:  -> needs patch
versions: +Python 2.7, Python 3.1, Python 3.2

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue9274>
_______________________________________


More information about the Python-bugs-list mailing list