On Sat, 03 Nov 2007 08:36:24 +0200, Hendrik van Rooyen wrote: > "Bruno Desthuilliers" wrote: > >>functions are *not* methods of their module. > > Now I am confused - if I write: > > result = foo.bar(param) > > Then if foo is a class, we probably all agree that bar is a method of > foo.
There are funny edge cases (e.g. bar might be an attribute with a __call__ method) but in general, yes, bar would be a method of foo. > But the same syntax would work if I had imported some module as foo. Same syntax, different semantics. Just like: "wholemeal bread sandwich" and "cloudy apple juice" have the same syntax but very different meanings: in the first one, the adjective wholemeal refers to the bread, not the entire sandwich, but in the second cloudy refers to the juice and not the apple. > So what's the difference ? Why can't bar be called a method of foo, or > is it merely a convention that classes have methods and modules have > functions? It is more than merely a convention. In Python, functions and methods are different things. They are very similar, but under the hood they are different: >>> def function(): ... pass ... >>> class Parrot(object): ... def method(self): ... pass ... >>> type(function) <type 'function'> >>> type(Parrot.method) <type 'instancemethod'> You might also like to call dir() on a function and a method and see how they differ. Methods are, in fact, lightweight wrappers around functions, and the underlying function can be found in the im_func attribute of the method. > Note that I am purposely refraining from mentioning a module that has a > class that has a method. But that isn't a problem. That is merely a module's class' method. -- Steven. -- http://mail.python.org/mailman/listinfo/python-list