Michael Torrie a écrit :
Steve Holden wrote:
You can think what you like, but there is a fundamental difference
between methods of a class and functions of a module. Until you
appreciate that you will likely make mistakes. Don't worry, though, we
all learn from our mistakes.
And this fundamental difference is?
That a method is bound to a class and (usually) an instance of this class.
From what I can tell an instance method is an object that encapsulates
the function object in a closure
s/closure/object/
that makes sure it has a reference to
"self."
the method object is instanciated by the function object itself when it
is looked up upon a class or instance (thanks to the descriptor
protocol). It (the method object) stores the function, the class and (if
bound) the instance as attributes, and it's __call__ method takes care
of calling the function with the instance as first positional argument.
I know that you dynamically add functions to objects creating
methods dynamically, by using new.instancemethod or something.
Or simply by manually invoking the descriptor protocol:
def func(self):
print "self is", self
class Foo(object):
pass
f = Foo()
f.func = func.__get__(f, type(f))
print dir(f.func)
This seems to indicate to me that there are functions and there are
functions. Methods are in fact functions, just with a callable wrapper
around them.
Methods *are* the callable wrapper around functions - so they can't be
functions themselves. From this POV, the functions wrapped by methods
are actually the *implementation* of the method.
--
http://mail.python.org/mailman/listinfo/python-list