Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!194.109.133.84.MISMATCH!newsfeed.xs4all.nl!newsfeed5.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.012 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'brilliant': 0.09; '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; 'def': 0.15; 'much!': 0.15; '1",': 0.16; 'val': 0.16; '>>>': 0.18; 'wrote': 0.20; 'function': 0.27; 'definition': 0.30; 'key,': 0.30; 'class': 0.30; 'to:addr:python- list': 0.33; '...': 0.34; 'header:User-Agent:1': 0.34; '(as': 0.34; 'header:X-Complaints-To:1': 0.35; 'thank': 0.35; 'received:org': 0.38; 'subject:: ': 0.39; 'user': 0.39; 'to:addr:python.org': 0.39; 'skip:d 20': 0.39; 'subject:from': 0.40; 'skip:n 30': 0.62; 'received:188': 0.68; 'want,': 0.71; '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:37:37 +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: 29 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1314689874 news.xs4all.nl 2403 [2001:888:2000:d::a6]:51050 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:12430 Jack Trades wrote in news:CAG5udOiOAge3uHrGSDTZ412GAg+CC- 6u8iGOyJ0Lnf3HnwUVzA@mail.gmail.com in gmane.comp.python.general: >> >>> 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 >> >> >> HTH, >> >> -- >> Arnaud >> > > That's brilliant and works flawlessly. Thank you very much! If an impementation (as you say up thread) can populate globals or locals with whatever they want, then how do you know that last item added was the function definition the user supplied ? Rob.