Method / Functions - What are the differences?

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Mon Mar 1 20:59:55 CET 2010


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 :-/



More information about the Python-list mailing list