Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!194.109.133.85.MISMATCH!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '(at': 0.03; 'exec': 0.07; 'python': 0.08; '>>>>': 0.09; 'foo': 0.09; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09; 'received:gator410.hostgator.com': 0.09; 'referenced': 0.09; 'statement.': 0.09; '~ethan~': 0.09; 'wrote:': 0.15; 'pm,': 0.16; 'def': 0.16; 'variable': 0.21; '(most': 0.21; 'header:In-Reply- To:1': 0.22; 'traceback': 0.25; 'statement': 0.25; 'skip:b 20': 0.26; 'sat,': 0.28; 'least': 0.31; 'chris': 0.32; 'does': 0.32; 'to:addr:python-list': 0.34; 'header:User-Agent:1': 0.34; 'however,': 0.34; "can't": 0.34; 'test': 0.34; 'assignment': 0.35; 'last):': 0.35; 'file': 0.36; 'subject:: ': 0.38; 'think': 0.38; 'to:addr:python.org': 0.39; 'received:websitewelcome.com': 0.65; 'received:184': 0.67; 'works,': 0.68; '30,': 0.74; '-->': 0.91 Date: Sat, 30 Jul 2011 14:22:36 -0700 From: Ethan Furman User-Agent: Thunderbird 2.0.0.24 (Windows/20100228) MIME-Version: 1.0 To: python-list@python.org Subject: Re: eval, exec and execfile dilemma References: <4E346028.5000302@shopzeus.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - gator410.hostgator.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - stoneleaf.us X-BWhitelist: no X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: c-67-170-168-84.hsd1.or.comcast.net ([192.168.74.5]) [67.170.168.84]:3228 X-Source-Auth: ethan+stoneleaf.us X-Email-Count: 3 X-Source-Cap: dG9idWs7dG9idWs7Z2F0b3I0MTAuaG9zdGdhdG9yLmNvbQ== X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 26 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1312061021 news.xs4all.nl 23926 [2001:888:2000:d::a6]:33520 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:10600 Chris Angelico wrote: > On Sat, Jul 30, 2011 at 8:48 PM, Laszlo Nagy wrote: >> the exec statement can be used to execute a def statement. However, I see no >> way to change the globals, so I cannot use the exec statement. > > A quick test in Python 2.4.5: >>>> exec "def foo():\n\tbar+=1\n\treturn 1\n" >>>> bar=2 >>>> foo() > Traceback (most recent call last): > File "", line 1, in ? > File "", line 2, in foo > UnboundLocalError: local variable 'bar' referenced before assignment This works, though (at least it does on 2.7): --> exec "def foo():\n\tglobal bar\n\tbar+=1\n\treturn 1\n" --> bar = 9 --> foo() 1 --> bar 10 Laszlo, why do you think you can't use exec? ~Ethan~