Path: csiph.com!aioe.org!news.stack.nl!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; 'attributes': 0.05; 'method,': 0.05; 'attribute': 0.07; 'exec': 0.07; 'bind': 0.09; 'dynamically': 0.09; 'def': 0.13; 'method.': 0.15; '*bound*': 0.16; ':o)': 0.16; 'a()': 0.16; 'behavior?': 0.16; 'cc:addr :python-list': 0.16; 'this:': 0.16; 'wrote:': 0.18; 'arguments': 0.18; 'converted': 0.18; 'instance': 0.18; 'help.': 0.19; 'cc:no real name:2**0': 0.21; 'header:In-Reply-To:1': 0.22; 'statement': 0.23; 'ignore': 0.24; 'cc:2**0': 0.26; 'function': 0.27; 'class': 0.29; 'worked': 0.29; 'print': 0.29; 'cc:addr:python.org': 0.29; 'cmd': 0.30; 'error': 0.30; 'does': 0.32; 'there': 0.33; 'header :User-Agent:1': 0.33; 'hi,': 0.34; 'url:python': 0.35; 'bound': 0.37; 'class.': 0.37; 'but': 0.37; 'using': 0.37; 'happens': 0.38; 'skip:_ 10': 0.38; "i'll": 0.38; 'getting': 0.38; 'url:org': 0.39; 'cannot': 0.39; 'kind': 0.62; 'works,': 0.68; 'appreciative': 0.84; 'url:datamodel': 0.84; 'url:reference': 0.84; 'wester': 0.84; 'following.': 0.91 X-IronPort-AV: E=Sophos;i="4.73,510,1325458800"; d="scan'208";a="205312" X-Virus-Scanned: amavisd-new at zimbra.sequans.com Date: Thu, 01 Mar 2012 14:46:43 +0100 From: Jean-Michel Pichavant User-Agent: Mozilla-Thunderbird 2.0.0.24 (X11/20100328) MIME-Version: 1.0 To: Rolf Wester Subject: Re: exec References: <4f4f7527$1@news.fhg.de> In-Reply-To: <4f4f7527$1@news.fhg.de> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit 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: 48 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1330609611 news.xs4all.nl 6945 [2001:888:2000:d::a6]:55583 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:21088 Rolf Wester wrote: > Hi, > > I would like to define methods using exec like this: > > class A: > def __init__(self): > cmd = "def sqr(self, x):\n return x**2\nself.sqr = sqr\n" > exec cmd > a = A() > print a.sqr(a, 2) > > This works, but I have to call sqr with a.sqr(a, 2), a.sqr(2) does not work > (TypeError: sqr() takes exactly 2 arguments (1 given)). > > Is there a possibility to define methods using exec and getting normal behavior? > > I would be very appreciative for any help. > > With kind regards > Rolf Wester > > > I'll try to ignore you are using the exec statement which is an error :o) a.sqr(2) would have worked only if sqr was a *bound* method. print a.sqr to oppose to print a.__init__ > You cannot dynamically bind a method, however you can do the following. cmd = "def sqr(x):\n return x**2\nself.sqr = sqr\n" http://docs.python.org/reference/datamodel.html : "It is also important to note that user-defined functions which are attributes of a class instance are not converted bound methods; this only happens when the function is an attribute of the class. " JM