MethodType/FunctionType and decorators

greg greg at cosc.canterbury.ac.nz
Thu Jul 5 04:32:49 CEST 2007


Alex Popescu wrote:
> - for decorators, my test is executed before the class is defined and
> so at that moment the function is still a function; it will become a
> methodinstance only after the class definition is closed

Closer, but still not quite right. Even after the class
is defined, the function sitting inside it is still
just an ordinary function. You can see this by looking
at

    A.__dict__['method']

An instancemethod is only created when you look the
method up in an instance, i.e.

    a = A()
    a.method

The instancemethod encapsulates a value for 'self' and
a reference to the underlying function. This is known
as a "bound method".

(You also get an instancemethod if you look the method
up in the class, i.e.

    A.method

but in that case the instancemethod doesn't contain
a value for 'self', and is known as an "unbound
method".)

--
Greg



More information about the Python-list mailing list