Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!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; 'exec': 0.07; 'python': 0.08; 'foo': 0.09; 'globals': 0.09; 'referenced': 0.09; 'statement.': 0.09; 'wrote:': 0.15; 'attribute,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; "function's": 0.16; 'pm,': 0.16; '>>>': 0.16; 'def': 0.16; 'meant': 0.17; "wouldn't": 0.17; 'exists': 0.19; 'received:74.125.82.174': 0.19; 'received:mail- wy0-f174.google.com': 0.19; 'variable': 0.21; '(most': 0.21; 'header:In-Reply-To:1': 0.22; 'dictionary': 0.23; 'traceback': 0.25; 'statement': 0.25; "i'm": 0.27; 'work.': 0.28; 'sat,': 0.28; 'message-id:@mail.gmail.com': 0.28; 'version': 0.30; 'equivalent': 0.31; "i've": 0.33; 'to:addr:python-list': 0.34; 'however,': 0.34; "can't": 0.34; 'test': 0.34; 'assignment': 0.35; 'last):': 0.35; 'file': 0.36; 'issue': 0.37; 'but': 0.37; 'received:google.com': 0.38; 'subject:: ': 0.38; 'received:74.125.82': 0.39; 'to:addr:python.org': 0.39; 'might': 0.39; 'received:74.125': 0.40; 'here': 0.66; '30,': 0.74 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=HoviThBEw7CjFMurIYtfGDBo00WAXJ/AXRpdFM+I9U0=; b=NyQOvbwwlbBHYYUtq05lf86SNZKe4NoPdZHzEOU1rAUkB3b2G689To170e887+jXaN h6YUE8g+w1XlWhqCFGC86teoceb+S+RPsMu6CvCoIZgWQMkD8fBrBIvWaFXMXmP4QYZc p7+TTeWKX3nyV8yq7ncyMkgsdF4Ny3qaxLszw= MIME-Version: 1.0 In-Reply-To: <4E346028.5000302@shopzeus.com> References: <4E346028.5000302@shopzeus.com> Date: Sat, 30 Jul 2011 21:43:32 +0100 Subject: Re: eval, exec and execfile dilemma From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 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: 29 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1312058615 news.xs4all.nl 23980 [2001:888:2000:d::a6]:54749 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:10599 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. You can't use: exec "blah blah" in globals() ? I've not used exec, nor its Python 3 equivalent exec(), much, so I don't know enough about it to know why this wouldn't work. But it's meant to be able to work with any dictionary for globals and/or locals. 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 Is that the issue you're up against? I'm thinking here that the solution might be the function's __globals__ attribute, but that only exists in Python 3 (I think). Can you upgrade/migrate to version 3? ChrisA