Instancemethod or function?

Viewed 11290

Today I defined a class just for testing what comes next:

class B(object):
    def p(self):
        print("p")

And later I did this:

>>> type(B.__dict__['p'])
<type 'function'>
>>> type(B.p)
<type 'instancemethod'>

So, why? Aren't B.p and B.__dict__['p'] the same object?

My astonishment just increased when a I tried this:

>>> B.__dict__['p']
<function p at 0x3d2bc80>
>>> type(B.__dict__['p'])
<type 'function'>

Ok, so far so good, the type is in both results function, but when I tried:

>>> B.p
<unbound method B.p>
>>> type(B.p)
<type 'instancemethod'>

What?!, Why? unbound method and instancemethod? Are those the same? Why two different names?

Well, It seems python is full of surprises!

And this is the python I'm using:

Python 2.7.4 (default, Sep 26 2013, 03:20:26) 
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
3 Answers
Related