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: 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 Subject: Re: [Q] How to exec code object with local variables specified? Date: Thu, 20 Sep 2012 12:18:00 -0400 References: 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: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 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