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


Groups > comp.lang.python > #29556

Re: [Q] How to exec code object with local variables specified?

Path csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'subject:: [': 0.03; 'context': 0.05; 'exec': 0.07; 'subject:code': 0.07; 'subject:How': 0.09; 'globals': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'terry': 0.09; 'def': 0.10; 'globals(),': 0.16; 'namespace.': 0.16; 'presume': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'runs.': 0.16; 'subject:object': 0.16; 'wrote:': 0.17; 'variables': 0.17; 'jan': 0.18; 'subject:] ': 0.19; 'trying': 0.21; 'context.': 0.22; 'defined': 0.22; 'pass': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'am,': 0.27; 'separate': 0.27; 'header:X-Complaints-To:1': 0.28; 'run': 0.28; 'definition': 0.29; 'objects': 0.29; 'class': 0.29; "i'm": 0.29; 'expect': 0.31; 'code': 0.31; 'running': 0.32; 'to:addr:python-list': 0.33; 'that,': 0.34; 'subject:?': 0.35; 'received:org': 0.36; 'but': 0.36; 'subject:with': 0.36; 'possible': 0.37; 'mean': 0.38; 'object': 0.38; 'nothing': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'within': 0.64; 'locals': 0.84; 'received:fios.verizon.net': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Terry Reedy <tjreedy@udel.edu>
Subject Re: [Q] How to exec code object with local variables specified?
Date Thu, 20 Sep 2012 12:18:00 -0400
References <CAFTm5Ru=E8Zii4k54fc4ii43wKPSwTq=iT4iRQCWy=46YiEZZw@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host pool-173-75-251-66.phlapa.fios.verizon.net
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20120824 Thunderbird/15.0
In-Reply-To <CAFTm5Ru=E8Zii4k54fc4ii43wKPSwTq=iT4iRQCWy=46YiEZZw@mail.gmail.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.962.1348157899.27098.python-list@python.org> (permalink)
Lines 30
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1348157899 news.xs4all.nl 6894 [2001:888:2000:d::a6]:42676
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:29556

Show key headers only | View raw


On 9/20/2012 7:27 AM, Makoto Kuwata wrote:

> Is it possible to run code object with local variables specified?

In the way you mean that, no.

> I'm trying the following code but not work:
>
>      def fn():
>         x = 1
>         y = 2
>      localvars = {'x': 0}
>      exec(fn.func_code, globals(), localvars)

The globals and locals you pass to exec are the globals and locals for 
the context in which the code object runs. They have nothing to do with 
the code objects local namespace.

Running exec with separate globals and locals is like running the code 
within a class definition context. If you ran

def fn(): x = 1
class dummy:
   fn()

dummy.x would not be defined and I presume you would not expect it to.

-- 
Terry Jan Reedy

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


Thread

Re: [Q] How to exec code object with local variables specified? Terry Reedy <tjreedy@udel.edu> - 2012-09-20 12:18 -0400

csiph-web