Distinguishing between functions and methods in a decorator.

Berteun Damman berteun at NO_SPAMdds.nl
Thu Feb 7 17:10:39 CET 2008


I was wondering a bit about the differences between methods and
functions. I have the following:

def wrap(arg):
    print type(arg)
    return arg

class C:
    def f():

    def g():

def h():

print type(C.f)
print type(h)

Which gives the following output:
<type 'function'>
<type 'instancemethod'>
<type 'function'>

The first line is caused by the 'wrap' function of course. I had
expected the first line to be 'instancemethod' too. So, I would guess,
these methods of C are first created as functions, and only then become
methods after they are 'attached' to some classobj. (You can do that
yourself of course, by saying, for example, C.h = h, then the type of
C.h is 'instancemethod' too.)

Why does the wrapping occur before the function is 'made' into an

The reason for asking is that I would like to differentiate between
wrapping a function and an instancemethod, because in the latter case,
the first parameter will be the implicit 'self', which I would like to
ignore.  However, when the wrapping occurs, the method still looks like
a function.


More information about the Python-list mailing list