types.UnboundMethodType is types.MethodType

Carlos Ribeiro carribeiro at gmail.com
Thu Oct 7 05:18:09 EDT 2004


On 6 Oct 2004 23:49:14 -0700, Michele Simionato
<michele.simionato at gmail.com> wrote:
> 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)
> 
> <....>

It's probably a good explanation, if we remember that Python went
through lots of changes since 1.5.2 days wrt to the typing system
*and* to the method dispatch system. But I don't think it's right,
because as it is now I can't reliably trust the results of:

isinstance(method, UnboundMethodType)

It's possible that today's implementation uses the same type, and what
changes is only the fact that a unbounded method hasn't still filled
some attributes. In this case, there is no sense to talk about a
UnboundMethodType. But *if* the types are different, then it should
reflect on the type tests, don't you think?

-- 
Carlos Ribeiro
Consultoria em Projetos
blog: http://rascunhosrotos.blogspot.com
blog: http://pythonnotes.blogspot.com
mail: carribeiro at gmail.com
mail: carribeiro at yahoo.com



More information about the Python-list mailing list