Prev: getting rpy2 from repository
Next: cpan for python?
From: Michael Rudolf on 28 Feb 2010 07:38 Out of curiosity I tried this and it actually worked as expected: >>> class T(object): x=[] foo=x.append def f(self): return self.x >>> t=T() >>> t.f() [] >>> T.foo(1) >>> t.f() [1] >>> At first I thought "hehe, always fun to play around with python. Might be useful sometimes" - but then It really confused me what I did. I mean: f is what we call a method, right? But was is foo? It is not a method and not a classmethod as it accepts no self and no cls. So that leaves staticmethod? OK, fair, as x is "static" here anyway this reflects what it does. But then consider this: >>> class T(object): def __init__(self): self.x=[] self.foo=self.x.append def f(self): return self.x >>> y=T() >>> y.x [] >>> y.foo(1) >>> y.x [1] >>> a=T() >>> a.x [] >>> a.foo(2) >>> a.x [2] >>> Note that all I did was moving the list and foo into the instance. Still no self and no cls, but also no static behaviour any more. So is foo just nothing of the above and really only a class/instance attribute which happens to be callable? Perhaps this all does not matter, but now I am really confused about the terminology. So: what makes a method a method? And of what type? Regards, Michael
From: Alf P. Steinbach on 28 Feb 2010 09:08 * Michael Rudolf: > Out of curiosity I tried this and it actually worked as expected: > > >>> class T(object): > x=[] > foo=x.append > def f(self): > return self.x > > > >>> t=T() > >>> t.f() > [] > >>> T.foo(1) > >>> t.f() > [1] > >>> > > At first I thought "hehe, always fun to play around with python. Might > be useful sometimes" - but then It really confused me what I did. I > mean: f is what we call a method, right? But was is foo? foo is (refers to) an object that supports call notation and that forwards calls somewhere else, in this case to append on a list. You might call it (descriptive) a call forwarder, or (C# or general terminology) a delegate, or (Python 2.x) a bound method. <example> >>> "Hello".upper <built-in method upper of str object at 0x00BA16E0> >>> f = "Hello".upper >>> f <built-in method upper of str object at 0x00BA16E0> >>> f() 'HELLO' >>> >>> >>> >>> f.__self__ 'Hello' >>> f.__call__ <method-wrapper '__call__' of builtin_function_or_method object at 0x00BDD170> >>> print( f.__doc__ ) S.upper() -> str Return a copy of S converted to uppercase. >>> _ </example> A common use for delegates is as command handlers in a GUI application, and in general for event notifications. Cheers & hth., - Alf
From: Rob Williscroft on 28 Feb 2010 09:24 Michael Rudolf wrote in news:hmdo3m$287$1(a)news.urz.uni-heidelberg.de in comp.lang.python: > Note that all I did was moving the list and foo into the instance. Still > no self and no cls, but also no static behaviour any more. Yes in the first case foo was an attribute of the class, and in the second an attribute of aon instance of the class. In both cases it was a bound method, something similar too: lambda item : T.x.append( item )
From: Michael Rudolf on 28 Feb 2010 11:39 Am 28.02.2010 15:08, schrieb Alf P. Steinbach: > >>> "Hello".upper > <built-in method upper of str object at 0x00BA16E0> > >>> f = "Hello".upper > >>> f > <built-in method upper of str object at 0x00BA16E0> > >>> f() > 'HELLO' > >>> > >>> > >>> > >>> f.__self__ > 'Hello' Holy hand grenade. You have no Idea how enlightened I feel right now :D Thank you, "bound method" was the term I forgot and your example... ....totally revealed the internals behind this to me. Especially the last line I quoted. I mean, I always knew *that* this works, but I never knew *why*. Regards, Michael
From: Bruno Desthuilliers on 1 Mar 2010 14:59
Michael Rudolf a �crit : > Out of curiosity I tried this and it actually worked as expected: > >>>> class T(object): > x=[] > foo=x.append > def f(self): > return self.x > > >>>> t=T() >>>> t.f() > [] >>>> T.foo(1) >>>> t.f() > [1] >>>> > > At first I thought "hehe, always fun to play around with python. Might > be useful sometimes" - but then It really confused me what I did. I > mean: f is what we call a method, right? Wrong. It's a function. T.f is an unbound method (in python 2.x at least) and t.f is a bound method. > But was is foo? A bound method. Bound to x, of course. > It is not a > method and not a classmethod as it accepts no self and no cls. Yes it does. Else how would t.foo(4) (or T.foo(4)) append 4 to x ? > Perhaps this all does not matter, It does. > but now I am really confused about the > terminology. So: what makes a method a method? The right question is: what makes a function a method !-) > And of what type? Answer here: http://groups.google.com/group/comp.lang.python/tree/browse_frm/thread/bd71264b6022765c/3a77541bf9d6617d#doc_89d608d0854dada0 I really have to put this in the wiki :-/ |