Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #67096

Re: exec and locals

From Alister <alister.ware@ntlworld.com>
Subject Re: exec and locals
Newsgroups comp.lang.python
References <530de8ed$0$29985$c3e8da3$5496439d@news.astraweb.com>
Message-ID <vsmPu.9$zx2.8@fx16.am4> (permalink)
Organization virginmedia.com
Date 2014-02-26 14:00 +0000

Show all headers | View raw


On Wed, 26 Feb 2014 13:15:25 +0000, Steven D'Aprano wrote:

> I have to dynamically generate some code inside a function using exec,
> but I'm not sure if it is working by accident or if I can rely on it.
> 
> Here is a trivial example:
> 
> 
> py> def spam():
> ...     exec( """x = 23""" )
> ...     return x ...
> py> spam()
> 23
> 
> 
> (My real example is more complex than this.)
> 
> According to the documentation of exec, I don't think this should
> actually work, and yet it appears to. The documentation says:
> 
>     The default locals act as described for function locals() below:
>     modifications to the default locals dictionary should not be
>     attempted. Pass an explicit locals dictionary if you need to see
>     effects of the code on locals after function exec() returns.
> 
> http://docs.python.org/3.4/library/functions.html#exec
> 
> 
> I *think* this means that if I want to guarantee that a local variable x
> is created by exec, I need to do this instead:
> 
> py> def eggs():
> ...     mylocals = {}
> ...     exec( """x = 23""", globals(), mylocals)
> ...     x = mylocals['x']
> ...     return x ...
> py> eggs()
> 23
> 
> The fact that it works in spam() above is perhaps an accident of
> implementation? Yes no maybe?

I have no idea but as exec is generally considered to be a bad idea are 
you absolutely sure this is the correct way to achieve your end goal?

perhaps if you detailed your requirement someone may be able to suggest a 
safer solution.


-- 
"Regardless of the legal speed limit, your Buick must be operated at
speeds faster than 85 MPH (140kph)."
-- 1987 Buick Grand National owners manual.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

exec and locals Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-02-26 13:15 +0000
  Re: exec and locals Chris Angelico <rosuav@gmail.com> - 2014-02-27 00:40 +1100
  Re: exec and locals Peter Otten <__peter__@web.de> - 2014-02-26 14:46 +0100
    Re: exec and locals Steven D'Aprano <steve@pearwood.info> - 2014-02-27 00:25 +0000
      Re: exec and locals Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-02-27 16:34 +1300
        Re: exec and locals Steven D'Aprano <steve@pearwood.info> - 2014-02-27 04:39 +0000
          Re: exec and locals Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-02-28 00:29 +1300
            Re: exec and locals Chris Angelico <rosuav@gmail.com> - 2014-02-27 22:41 +1100
            Re: exec and locals Steven D'Aprano <steve@pearwood.info> - 2014-02-28 01:49 +0000
      Re: exec and locals Dan Sommers <dan@tombstonezero.net> - 2014-02-27 03:47 +0000
      Re: exec and locals Dave Angel <davea@davea.name> - 2014-02-26 23:20 -0500
        Re: exec and locals Steven D'Aprano <steve@pearwood.info> - 2014-02-27 04:47 +0000
          Re: exec and locals Chris Angelico <rosuav@gmail.com> - 2014-02-27 16:05 +1100
  Re: exec and locals Peter Otten <__peter__@web.de> - 2014-02-26 14:55 +0100
  Re: exec and locals Alister <alister.ware@ntlworld.com> - 2014-02-26 14:00 +0000
    Re: exec and locals Steven D'Aprano <steve@pearwood.info> - 2014-02-27 00:31 +0000
      Re: exec and locals Alister <alister.ware@ntlworld.com> - 2014-02-27 09:59 +0000

csiph-web