types.UnboundMethodType is types.MethodType

Michele Simionato michele.simionato at gmail.com
Thu Oct 7 02:49:14 EDT 2004


Carlos Ribeiro <carribeiro at gmail.com> wrote in message news:<mailman.4467.1097114079.5135.python-list at python.org>...
> Just curious. I was trying to test for a class method in some code of
> mine, and stumbled on a few things that I really could not understand:
> 
> # C is a class, m is a class method
> >>> c.m
>  <bound method C.m of <__main__.C instance at 0x0120E350>>
> >>> isinstance(c.m, types.UnboundMethodType)
> True
> # C is a class, cm is a class method
> >>> C.cm
>  <bound method classobj.cm of <class __main__.C at 0x0120D870>>
> >>> isinstance(C.cx, types.UnboundMethodType)
>  True
> >>> types.UnboundMethodType is types.MethodType
>  True
> >>> types.UnboundMethodType, types.MethodType
>  (<type 'instancemethod'>, <type 'instancemethod'>)
> >>> id(types.UnboundMethodType), id(types.MethodType)
> (504034256, 504034256)
> 
> I don't get it. Why to have two different identifiers that are in fact the same?

Backward compatibility?

Here is from the source of types.py:

<...>

class _C:
    def _m(self): pass
ClassType = type(_C)
UnboundMethodType = type(_C._m)         # Same as MethodType
_x = _C()
InstanceType = type(_x)
MethodType = type(_x._m)

<....>


              Michele Simionato



More information about the Python-list mailing list