Re: Attaching functions to objects as methods

2006-07-07 Thread Steven Bethard
John Machin wrote: > Injecting a "private" method into a particular instance is not much more > complicated: > > >>> def own(self, arg): > ...print "own" > ...self.ozz = arg > ... > >>> p = K() > >>> import types > >>> p.metho = types.MethodType(own, p) > >>> p.metho("plugh") > own >

Re: Attaching functions to objects as methods

2006-07-07 Thread John Machin
On 8/07/2006 11:01 AM, tac-tics wrote: > Experimenting, I found that > x.fun = lambda: fun(x) > > works as well. Any comments on this way? > Appears to work. Less typing for a no-argument method, but you need to specify the argument list *twice* compared with *zero* times with the types

Re: Attaching functions to objects as methods

2006-07-07 Thread tac-tics
Experimenting, I found that >>> x.fun = lambda: fun(x) works as well. Any comments on this way? -- http://mail.python.org/mailman/listinfo/python-list

Re: Attaching functions to objects as methods

2006-07-07 Thread John Machin
On 8/07/2006 9:29 AM, tac-tics wrote: > Python is a crazy language when it comes to object versatility. I know > I can do: > class test: > ...def __init__(self): > ... pass x = test() def fun(): > ... print "fun" x.fun = fun x.fun() > fun > > However, expe

Re: Attaching functions to objects as methods

2006-07-07 Thread tac-tics
> Functions are descriptors[1], and their __get__ method is used to bind > them to a particular instance:: Thank you muchly. -- http://mail.python.org/mailman/listinfo/python-list

Re: Attaching functions to objects as methods

2006-07-07 Thread Steven Bethard
tac-tics wrote: > Python is a crazy language when it comes to object versatility. I know > I can do: > class test: > ...def __init__(self): > ... pass x = test() def fun(): > ... print "fun" x.fun = fun x.fun() > fun > > However, experimenting shows that t

Attaching functions to objects as methods

2006-07-07 Thread tac-tics
Python is a crazy language when it comes to object versatility. I know I can do: >>> class test: ...def __init__(self): ... pass >>> x = test() >>> def fun(): ... print "fun" >>> x.fun = fun >>> x.fun() fun >>> However, experimenting shows that these attached functions are not bou