[docs] [issue16851] Hint about correct ismethod and isfunction usage

Greg Couch report at bugs.python.org
Tue Feb 12 01:07:09 CET 2013


Greg Couch added the comment:

In my opinion, the Python 2.7 results are wrong.

In Python 2.7, inspect.ismethod returns True for both bound and unbound methods -- ie., is broken according to the documentation.  As a workaround, I'm using:

def is_bound_method(obj):
    return hasattr(obj, '__self__') and obj.__self__ is not None

is_bound_method also works for methods of classes implemented in C, e.g., int:

>>> a = 1
>>> is_bound_method(a.__add__)
True
>>> is_bound_method(int.__add__)
False

But is not very useful in that case because inspect.getargspec does not work for functions implemented in C.

is_bound_method works unchanged in Python 3, but as noted above, in Python 3, inspect.ismethod properly distinguishes between bound and unbound methods, so it is not necessary.

----------
nosy: +gregcouch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue16851>
_______________________________________


More information about the docs mailing list