Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #21088
| 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 | <prvs=400c04894=jeanmichel@sequans.com> |
| 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 <jeanmichel@sequans.com> |
| User-Agent | Mozilla-Thunderbird 2.0.0.24 (X11/20100328) |
| MIME-Version | 1.0 |
| To | Rolf Wester <rolf.wester@ilt.fraunhofer.de> |
| 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 <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.320.1330609611.3037.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
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 <function sqr at 0x94ecd14> to oppose to print a.__init__ <bound method A.__init__ of <__main__.A instance at 0x948734c>> 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 : <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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
exec Rolf Wester <rolf.wester@ilt.fraunhofer.de> - 2012-03-01 14:07 +0100
Re: exec Jean-Michel Pichavant <jeanmichel@sequans.com> - 2012-03-01 14:46 +0100
Re: exec Arnaud Delobelle <arnodel@gmail.com> - 2012-03-01 14:13 +0000
Re: exec Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-03-01 14:21 +0000
Re: exec Rolf Wester <rolf.wester@ilt.fraunhofer.de> - 2012-03-01 16:58 +0100
Re: exec Michael Ströder <michael@stroeder.com> - 2012-03-01 17:26 +0100
Re: exec Peter Otten <__peter__@web.de> - 2012-03-01 18:14 +0100
Re: exec Rolf Wester <rolf.wester@ilt.fraunhofer.de> - 2012-03-01 18:23 +0100
Re: exec Peter Otten <__peter__@web.de> - 2012-03-02 08:26 +0100
csiph-web