[Tutor] Function assignment (was: Re: Function type?)

Zak Arntson zak@harlekin-maus.com
Thu Jun 12 09:47:01 2003


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)
>
> # 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.
>
> --
> Zak Arntson
> www.harlekin-maus.com - Games - Lots of 'em


-- 
Zak Arntson
www.harlekin-maus.com - Games - Lots of 'em