[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>
&gt; Okay ... so I'm following things so far. MakeMsg is neat function!
Now<br>
&gt; I'm trying to wrap my head around the instancemethod function.
Here's my<br>
&gt; situation:<br>
&gt;<br>
&gt; I have an item class (for things like pipes) that needs to allow
for<br>
&gt; methods to be dynamically added to it. So here's my totally
unworking<br>
&gt; code:<br>
&gt;<br>
&gt; ###<br>
&gt; import new<br>
&gt;<br>
&gt; class Item:<br>
&gt;&nbsp;&nbsp;&nbsp;&nbsp; def attach(self, name, func):<br>
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 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>&gt;<br>
&gt; # Example:<br>
&gt;<br>
&gt; pipe = Item()<br>
&gt;<br>
&gt; def pipe_desc(self):<br>
&gt;&nbsp;&nbsp;&nbsp; if self.location == player:<br>
&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return &quot;You've got
it!&quot;<br>
&gt;&nbsp;&nbsp;&nbsp; return &quot;No dice&quot;<br>
&gt;<br>
&gt; pipe.attach('desc', pipe_desc)<br>
&gt;<br>
&gt; # And here's a makeMsg, just for fun<br>
&gt; pipe.attach('longdesc', makeMsg(&quot;Here's a long
description!!&quot;))<br>
&gt;<br>
&gt; ###<br>
&gt;<br>
&gt; Does that make sense? I'm trying to attach a new method
&quot;desc&quot; to the<br>
&gt; pipe Item. Only the code completely breaks.</blockquote><br>
The manual says
&quot;<a name="l2h-646"></a><tt>instancemethod</tt>(function, instance,
class) This function will return a method object, bound to
instance....&quot;.<br><br>
This suggests: <br><br>
class Item:<br>
&nbsp;&nbsp;&nbsp; def attach(self, name, func):<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 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=======--