Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'assign': 0.04; '(python': 0.05; 'suppose': 0.05; 'exec': 0.07; '""")': 0.09; 'globals': 0.09; 'that).': 0.09; 'def': 0.15; '1",': 0.16; 'src,': 0.16; 'test()': 0.16; 'test():': 0.16; 'val': 0.16; 'cc:addr:python- list': 0.16; 'looked': 0.16; 'mon,': 0.16; 'this:': 0.16; 'wrote:': 0.16; '>>>': 0.18; 'string,': 0.18; 'cc:no real name:2**0': 0.20; 'seems': 0.20; 'wrote': 0.20; 'header:In-Reply- To:1': 0.22; '(though': 0.23; 'produces': 0.23; 'pm,': 0.24; 'aug': 0.24; 'function': 0.27; 'problem': 0.28; 'message- id:@mail.gmail.com': 0.29; 'cc:addr:python.org': 0.30; 'key,': 0.30; 'class': 0.30; 'does': 0.32; 'it.': 0.33; 'there': 0.33; 'rules': 0.34; '...': 0.34; 'doing': 0.36; 'cc:2**1': 0.36; 'but': 0.37; 'received:google.com': 0.38; 'received:209.85': 0.38; 'subject:: ': 0.39; 'manually': 0.39; 'user': 0.39; 'skip:d 20': 0.39; 'subject:from': 0.40; 'more': 0.60; 'your': 0.61; 'double': 0.61; 'here': 0.65; '"for': 0.67; '29,': 0.67; 'august': 0.70; '12:30': 0.84; 'locals': 0.84; 'subject:value': 0.84; 'received:209.85.218.46': 0.91; 'received:mail- yi0-f46.google.com': 0.91; 'subject:better': 0.93 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 :cc:content-type:content-transfer-encoding; bh=CFW7UHm4vB7BKgcgzjaD4Uqk81/UCBO42uOx9OKZZAs=; b=xIBvMfrGkuvWkr5XQusoyuN61+VUJ3a8rlyw+QMvdPwZ2Qt3qyOayKpmb1ps/0EoBz Ve5GdO4yxRToh0pVOT1Pfmwp7EDBPaA/wYSUONogqBR6PobVnbwkTTexDU/QAgUTihZH OaTrNjasDk3OtvjmiC2iZmF6j3uwl1IHHQIFY= MIME-Version: 1.0 In-Reply-To: References: Date: Mon, 29 Aug 2011 23:50:46 +0100 Subject: Re: Returning a value from exec or a better solution From: Arnaud Delobelle To: Jack Trades Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org, Rob Williscroft 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: 58 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1314658248 news.xs4all.nl 2532 [2001:888:2000:d::a6]:39392 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:12423 On 29 August 2011 23:14, Jack Trades wrote: > On Mon, Aug 29, 2011 at 12:30 PM, Rob Williscroft wrote: >> >> Jack Trades wrote in >> > ... I wanted to allow the user to manually return the >> > function from the string, like this: >> > >> > a =3D exec(""" >> > def double(x): >> > =C2=A0 return x * 2 >> > double >> > """) >> > >> > However it seems that exec does not return a value as it produces a >> > SyntaxError whenever I try to assign it. >> >> def test(): >> =C2=A0src =3D ( >> =C2=A0 =C2=A0 =C2=A0"def double(x):" >> =C2=A0 =C2=A0 =C2=A0" =C2=A0return x * 2" >> =C2=A0 =C2=A0) >> =C2=A0globals =C2=A0=3D {} >> =C2=A0exec( src, globals ) >> =C2=A0return globals[ "double" ] >> >> print( test() ) > > I looked into doing it that way but it still requires that the user use a > specific name for the function they are defining.=C2=A0 The docs on exec = say that > an implementation may populate globals or locals with whatever they want = so > that also rules out doing a simple "for item in globals", as there may be > more than just the one function in there (though I suppose I may be able = to > work around that). Hi Jack, Here is a possible solution for your problem (Python 3): >>> class CapturingDict(dict): ... def __setitem__(self, key, val): ... self.key, self.val =3D key, val ... dict.__setitem__(self, key, val) ... >>> c =3D CapturingDict() >>> exec("def myfunction(x): return 1", c) >>> c.key 'myfunction' >>> c.val HTH, --=20 Arnaud