[Tutor] The trap of the year

Steven D'Aprano steve at pearwood.info
Wed Jan 26 00:49:32 CET 2011


Corey Richardson wrote:

> To be pedantic, a method _is_ a function, just under the umbrella of a
> class, with it's parent object being passed to it.

To be even more pedantic, a method object is a wrapper (technically, a 
descriptor) around a function object. It's also slightly different 
between Python 2 and Python 3. Python 2 has bound and unbound method 
wrappers, depending on whether you call class.method or instance.method, 
but Python 3 gets rid of unbound methods and just returns the function 
object when you call class.method.

And of course, there are also "class methods" and "static methods", as 
well as custom-built method types.

Descriptors are fundamental to Python, but they're for advanced users. 
You can treat methods as just functions, except that they automatically 
get the first argument (usually called "self") automatically supplied.


-- 
Steven


More information about the Tutor mailing list