Path: csiph.com!aioe.org!newsfeed.x-privat.org!news2.euro.net!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.048 X-Spam-Evidence: '*H*': 0.90; '*S*': 0.00; 'exec': 0.07; 'received:209.85.214.174': 0.13; '13:07,': 0.16; 'a()': 0.16; 'cc:addr:python-list': 0.16; 'this:': 0.16; 'wrote:': 0.18; 'arguments': 0.18; 'cc:no real name:2**0': 0.21; 'header:In-Reply- To:1': 0.22; 'cc:2**0': 0.26; "i'm": 0.28; 'message- id:@mail.gmail.com': 0.29; 'class': 0.29; 'print': 0.29; 'cc:addr:python.org': 0.29; 'cmd': 0.30; 'does': 0.32; 'this.': 0.33; 'hi,': 0.34; 'curious': 0.34; 'received:209.85.214': 0.36; 'but': 0.37; 'received:google.com': 0.37; 'using': 0.37; 'skip:_ 10': 0.38; 'received:209.85': 0.38; 'doing': 0.38; 'received:209': 0.39; 'header:Received:6': 0.61; 'your': 0.61; 'march': 0.61; 'works,': 0.68; 'wester': 0.84 Received-SPF: pass (google.com: domain of arnodel@gmail.com designates 10.182.75.40 as permitted sender) client-ip=10.182.75.40; Authentication-Results: mr.google.com; spf=pass (google.com: domain of arnodel@gmail.com designates 10.182.75.40 as permitted sender) smtp.mail=arnodel@gmail.com; dkim=pass header.i=arnodel@gmail.com 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=/WsGcRHZk/1a8a2wEJUzwlHHwfkqoMfgOELFZPAgyow=; b=sq6siu+uIMyJWa54cqI7oBMVjsEpowLV75Wdm2GntWkZy0DlGkmGRAgf1zQv5HQL56 hjK17jfJh+C9SAdGADtblQcNJ6D8if4q3Lvyhq9nEXxwAUrmn/o8QyUdY6iM7hO5Xozz woc0E1G+yVu1Cq7sgT89wqKoq1LidN/fVjCSY= MIME-Version: 1.0 In-Reply-To: <4f4f7527$1@news.fhg.de> References: <4f4f7527$1@news.fhg.de> Date: Thu, 1 Mar 2012 14:13:39 +0000 Subject: Re: exec From: Arnaud Delobelle To: Rolf Wester Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org 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: 21 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1330611223 news.xs4all.nl 6978 [2001:888:2000:d::a6]:36433 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:21092 On 1 March 2012 13:07, Rolf Wester wrote: > Hi, > > I would like to define methods using exec like this: > > class A: > =C2=A0 =C2=A0def __init__(self): > =C2=A0 =C2=A0 =C2=A0 =C2=A0cmd =3D "def sqr(self, x):\n =C2=A0 =C2=A0retu= rn x**2\nself.sqr =3D sqr\n" > =C2=A0 =C2=A0 =C2=A0 =C2=A0exec cmd > a =3D A() > print a.sqr(a, 2) > > This works, but I have to call sqr with a.sqr(a, 2), a.sqr(2) does not wo= rk > (TypeError: sqr() takes exactly 2 arguments (1 given)). I'm curious to know your motivation for doing this. --=20 Arnaud