[Tutor] Function assignment (was: Re: Function type?)
Bob Gailer
bgailer@alum.rpi.edu
Thu Jun 12 13:48:01 2003
--=======582A30F2=======
Content-Type: multipart/alternative; x-avg-checked=avg-ok-17912FEA; boundary="=====================_12258356==.ALT"
--=====================_12258356==.ALT
Content-Type: text/plain; x-avg-checked=avg-ok-17912FEA; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 8bit
At 06:46 AM 6/12/2003 -0700, Zak Arntson wrote:
>Oops ... accidentally sent this privately. I have to change my mailing
>list settings or something :)
>
> > Okay ... so I'm following things so far. MakeMsg is neat function! Now
> > I'm trying to wrap my head around the instancemethod function. Here's my
> > situation:
> >
> > I have an item class (for things like pipes) that needs to allow for
> > methods to be dynamically added to it. So here's my totally unworking
> > code:
> >
> > ###
> > import new
> >
> > class Item:
> > def attach(self, name, func):
> > eval('self.%s = new.instancemethod(func, self, Item)' % name)
eval is for expressions. Alas, assignment is a statement rather than an
expression.
Try exec instead.
> >
> > # Example:
> >
> > pipe = Item()
> >
> > def pipe_desc(self):
> > if self.location == player:
> > return "You've got it!"
> > return "No dice"
> >
> > pipe.attach('desc', pipe_desc)
> >
> > # And here's a makeMsg, just for fun
> > pipe.attach('longdesc', makeMsg("Here's a long description!!"))
> >
> > ###
> >
> > Does that make sense? I'm trying to attach a new method "desc" to the
> > pipe Item. Only the code completely breaks.
The manual says "instancemethod(function, instance, class) This function
will return a method object, bound to instance....".
This suggests:
class Item:
def attach(self, name, func):
new.instancemethod(func, self, Item)
Bob Gailer
bgailer@alum.rpi.edu
303 442 2625
--=====================_12258356==.ALT
Content-Type: text/html; x-avg-checked=avg-ok-17912FEA; charset=us-ascii
Content-Transfer-Encoding: 8bit
<html>
<body>
At 06:46 AM 6/12/2003 -0700, Zak Arntson wrote:<br><br>
<blockquote type=cite class=cite cite>Oops ... accidentally sent this
privately. I have to change my mailing<br>
list settings or something :)<br><br>
> Okay ... so I'm following things so far. MakeMsg is neat function!
Now<br>
> I'm trying to wrap my head around the instancemethod function.
Here's my<br>
> situation:<br>
><br>
> I have an item class (for things like pipes) that needs to allow
for<br>
> methods to be dynamically added to it. So here's my totally
unworking<br>
> code:<br>
><br>
> ###<br>
> import new<br>
><br>
> class Item:<br>
> def attach(self, name, func):<br>
> eval('self.%s =
new.instancemethod(func, self, Item)' % name)</blockquote><br>
eval is for expressions. Alas, assignment is a statement rather than an
expression.<br>
Try exec instead.<br><br>
<blockquote type=cite class=cite cite>><br>
> # Example:<br>
><br>
> pipe = Item()<br>
><br>
> def pipe_desc(self):<br>
> if self.location == player:<br>
> return "You've got
it!"<br>
> return "No dice"<br>
><br>
> pipe.attach('desc', pipe_desc)<br>
><br>
> # And here's a makeMsg, just for fun<br>
> pipe.attach('longdesc', makeMsg("Here's a long
description!!"))<br>
><br>
> ###<br>
><br>
> Does that make sense? I'm trying to attach a new method
"desc" to the<br>
> pipe Item. Only the code completely breaks.</blockquote><br>
The manual says
"<a name="l2h-646"></a><tt>instancemethod</tt>(function, instance,
class) This function will return a method object, bound to
instance....".<br><br>
This suggests: <br><br>
class Item:<br>
def attach(self, name, func):<br>
new.instancemethod(func, self,
Item)<br><br>
<x-sigsep><p></x-sigsep>
Bob Gailer<br>
bgailer@alum.rpi.edu<br>
303 442 2625<br>
</body>
</html>
--=====================_12258356==.ALT--
--=======582A30F2=======
Content-Type: text/plain; charset=us-ascii; x-avg=cert; x-avg-checked=avg-ok-17912FEA
Content-Disposition: inline
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.488 / Virus Database: 287 - Release Date: 6/5/2003
--=======582A30F2=======--