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


Groups > comp.lang.python > #12423

Re: Returning a value from exec or a better solution

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 <arnodel@gmail.com>
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 <CAG5udOh1+oE4g9Frjp3pucbHUtWcN34KK35a-Xs2YqkZH9X5=w@mail.gmail.com>
References <CAG5udOg=GtFGPmTB=1OJNvNRPdYUcxDoKN1WJQMOMv9gx0+fZA@mail.gmail.com> <Xns9F50BCB7B5708rtwfreenetREMOVEcouk@80.91.229.10> <CAG5udOh1+oE4g9Frjp3pucbHUtWcN34KK35a-Xs2YqkZH9X5=w@mail.gmail.com>
Date Mon, 29 Aug 2011 23:50:46 +0100
Subject Re: Returning a value from exec or a better solution
From Arnaud Delobelle <arnodel@gmail.com>
To Jack Trades <jacktradespublic@gmail.com>
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
Cc python-list@python.org, Rob Williscroft <rtw@rtw.me.uk>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
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.559.1314658248.27778.python-list@python.org> (permalink)
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

Show key headers only | View raw


On 29 August 2011 23:14, Jack Trades <jacktradespublic@gmail.com> wrote:
> On Mon, Aug 29, 2011 at 12:30 PM, Rob Williscroft <rtw@rtw.me.uk> wrote:
>>
>> Jack Trades wrote in
>> > ... I wanted to allow the user to manually return the
>> > function from the string, like this:
>> >
>> > a = exec("""
>> > def double(x):
>> >   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():
>>  src = (
>>      "def double(x):"
>>      "  return x * 2"
>>    )
>>  globals  = {}
>>  exec( src, globals )
>>  return 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.  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 = key, val
...         dict.__setitem__(self, key, val)
...
>>> c = CapturingDict()
>>> exec("def myfunction(x): return 1", c)
>>> c.key
'myfunction'
>>> c.val
<function myfunction at 0x100634d10>

HTH,

-- 
Arnaud

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


Thread

Re: Returning a value from exec or a better solution Arnaud Delobelle <arnodel@gmail.com> - 2011-08-29 23:50 +0100

csiph-web