Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'string.': 0.04; 'suppose': 0.05; 'exec': 0.07; 'globals': 0.09; 'received:188.220': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'that).': 0.09; 'def': 0.15; 'last,': 0.16; 'src,': 0.16; 'test()': 0.16; 'test():': 0.16; 'looked': 0.16; 'wrote': 0.20; '(or': 0.23; '(though': 0.23; 'extract': 0.24; 'function': 0.27; 'definition': 0.30; 'expression': 0.32; 'source': 0.33; 'there': 0.33; 'to:addr :python-list': 0.33; 'rules': 0.34; 'header:User-Agent:1': 0.34; 'header:X-Complaints-To:1': 0.35; 'regular': 0.35; 'doing': 0.36; 'but': 0.37; 'received:org': 0.38; 'subject:: ': 0.39; 'user': 0.39; 'why': 0.39; 'to:addr:python.org': 0.39; 'subject:from': 0.40; 'more': 0.60; '"for': 0.67; 'received:188': 0.68; 'locals': 0.84; 'received:bethere.co.uk': 0.84; 'subject:value': 0.84; 'subject:better': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Rob Williscroft Subject: Re: Returning a value from exec or a better solution Date: Tue, 30 Aug 2011 07:28:41 +0000 (UTC) References: X-Gmane-NNTP-Posting-Host: 188-220-43-229.zone11.bethere.co.uk User-Agent: Xnews/5.04.25 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: 30 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1314689337 news.xs4all.nl 2421 [2001:888:2000:d::a6]:50444 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:12429 Jack Trades wrote in news:CAG5udOh1+oE4g9Frjp3pucbHUtWcN34KK35a-Xs2YqkZH9X5=w@mail.gmail.com in gmane.comp.python.general: >> 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). > > Why not just get the name from the user, or use a regular expression to extract the first (or last, maybe) definition from the source string. Rob.