UnboundMethodType and MethodType
Kirk McDonald
mooquack at suad.org
Tue Feb 7 22:24:48 EST 2006
Schüle Daniel wrote:
> Hello all,
>
> >>> class Q:
> ... def bar(self):
> ... pass
> ...
> >>> import types
> >>> types.UnboundMethodType is types.MethodType
> True
> >>>
> >>> type(Q.bar)
> <type 'instancemethod'>
> >>>
> >>> q = Q()
> >>> type(q.bar)
> <type 'instancemethod'>
> >>>
> >>> type(q.bar) is types.UnboundMethodType
> True
> >>> q.bar
> <bound method Q.bar of <__main__.Q instance at 0x4042756c>>
> >>>
>
> I think is not very consistent
> notice q.bar is bounded although type(q.bar)
> says it's types.UnboundedMethodType
> what do you think?
>
> Regard, Daniel
>
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. It's simply called in different ways. You can
just as easily say:
>>> B.bar(b)
As:
>>> b.bar()
-Kirk McDonald
More information about the Python-list
mailing list