UnboundMethodType and MethodType

Kent Johnson kent at kentsjohnson.com
Wed Feb 8 09:17:08 EST 2006


Kirk McDonald wrote:

> I think it's perfectly consistent:
> 
>  >>> class B(object):
> ...     def bar(self): pass
> ...
>  >>> B.bar
> <unbound method B.bar>
>  >>> type(B.bar)
> <type 'instancemethod'>
>  >>> b = B()
>  >>> b.bar
> <bound method B.bar of <__main__.B object at 0xb7bd544c>>
>  >>> type(b.bar)
> <type 'instancemethod'>
>  >>> id(B.bar)
> -1211888788
>  >>> id(b.bar)
> -1211888788
> 
> It's the same function, whether it's bound or not. Thus, it should 
> always have the same type. 

No, it's not the same function. You got the same id because you didn't 
bind B.bar and b.bar to anything so the id was reused.

  >>> class B(object):
  ...   def bar(self): pass
  ...
  >>> Bbar = B.bar
  >>> bbar = B().bar
  >>> Bbar
<unbound method B.bar>
  >>> bbar
<bound method B.bar of <__main__.B object at 0x008759B0>>
  >>> id(Bbar)
10751312
  >>> id(bbar)
10736624
  >>> Bbar is bbar
False

Kent



More information about the Python-list mailing list