Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'ast': 0.09; 'nameerror:': 0.09; 'subject:()': 0.09; '{},': 0.09; 'def': 0.10; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'literals.': 0.16; 'namespace.': 0.16; 'oct': 0.16; 'wrote:': 0.17; 'odd': 0.17; 'thu,': 0.17; 'variable': 0.20; 'skip:" 30': 0.20; 'defined': 0.22; 'skip:_ 20': 0.22; 'statement': 0.23; 'seems': 0.23; 'header:In-Reply-To:1': 0.25; 'skip:" 20': 0.26; 'message-id:@mail.gmail.com': 0.27; 'issues.': 0.29; 'skip:_ 10': 0.29; 'van': 0.29; 'class': 0.29; 'instead,': 0.33; 'to:addr :python-list': 0.33; 'received:google.com': 0.34; 'adds': 0.35; 'pm,': 0.35; "won't": 0.35; 'received:209.85': 0.35; 'but': 0.36; 'anything': 0.36; 'should': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'think': 0.40; 'want,': 0.65 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=/fzTcZtIXeBrEkJGSYtpviuxDvPRCd4FOfOo2yk3QXQ=; b=LSqnFbyXT2ydvYnIEHM3LGtMha3zMBFMfK8v1PzlaYDV7yoAiBUcqiS9EFQN1ToV61 daog2isUwOwnz27+N6UJzHMZsPcbRITMwzke2be/xz34QkC/Sj7iUP3aFAuRlwjE4vfG xEsiIoT4U/RFfxMGpAO30M3rsLzzxYHhb4n0GdHFO2mXqErkfyDx9mRaoaAI82x+HAHK ulSiQViUjkyRNtI5U30QOVMTxl9XIBy5hY9duUCryjK0gDHliWmDbhcCZOf805qXssZS 8zr9qbs+T+Uc9aFmvtfRRUiaPRpou+5KuC7iA1AGc1NQ9r08yjckRldxKagkfzs+iein Vm2g== MIME-Version: 1.0 In-Reply-To: <2f12fa83-54cc-4fc2-85e4-b8aebebf4242@googlegroups.com> References: <2f12fa83-54cc-4fc2-85e4-b8aebebf4242@googlegroups.com> Date: Thu, 18 Oct 2012 22:49:31 +1100 Subject: Re: use of exec() 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.15 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: 22 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1350560975 news.xs4all.nl 6971 [2001:888:2000:d::a6]:58837 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:31629 On Thu, Oct 18, 2012 at 10:41 PM, lars van gemerden wrote: > NameError: name 'function' is not defined > > which seems an odd error, but i think some global variable is necessary for this to work (if i put in globals() instead of {}, it works). The def statement simply adds a name to the current namespace. This should work (untested): class _functioncode(code): def _creat_func_(self): ns={} exec("def function(%s):\n\t%s" % (", ".join(type(self).args), "\n\t".join(self.split('\n'))),ns,ns) return ns.function But it's going to be eternally plagued by security issues. You may want, instead, to look at literal_eval from the ast module; but that won't work if you need anything other than, as the name suggests, literals. ChrisA