A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2010-May/100384.html below:

[Python-Dev] variable name resolution in exec is incorrect

[Python-Dev] variable name resolution in exec is incorrectColin H hawkett at gmail.com
Thu May 27 02:53:15 CEST 2010
Hi Guido,

   Thanks for the possible workaround - unfortunately 'stuff' will
contain a whole stack of things that are not in 'context', and were
not defined in 'user_code' - things that python embeds - a (very
small) selection -

{..., 'NameError': <type 'exceptions.NameError'>, 'BytesWarning':
<type 'exceptions.BytesWarning'>, 'dict': <type 'dict'>, 'input':
<function input at 0x10047a9b0>, 'oct': <built-in function oct>,
'bin': <built-in function bin>, ...}

It makes sense why this happens of course, but upon return, the
globals dict is very large, and finding the stuff you defined in your
user_code amongst it is a very difficult task.  Avoiding this problem
is the 'locals' use-case for me.  Cheers,

Colin

On Thu, May 27, 2010 at 1:38 AM, Guido van Rossum <guido at python.org> wrote:
> This is not easy to fix. The best short-term work-around is probably a
> hack like this:
>
> def define_stuff(user_code):
>  context = {...}
>  stuff = {}
>  stuff.update(context)
>  exec(user_code, stuff)
>  for key in context:
>    if key in stuff and stuff[key] == context[key]:
>      del stuff[key]
>  return stuff
>
> --
> --Guido van Rossum (python.org/~guido)
>
More information about the Python-Dev mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4