Method / Functions - What are the differences?

Bruno Desthuilliers bdesth.quelquechose at
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.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 (or 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:

I really have to put this in the wiki :-/

More information about the Python-list mailing list