Skip to content Skip to sidebar Skip to footer

Detecting Bound Method In Classes (not Instances) In Python 3

Given a class C with a function or method f, I use inspect.ismethod(obj.f) (where obj is an instance of C) to find out if f is bound method or not. Is there a way to do the same di

Solution 1:

There are no unbound methods in Python 3, so you cannot detect them either. All you have is regular functions. At most you can see if they have a qualified name with a dot, indicating that they are nested, and their first argument name is self:

if'.'in method.__qualname__ and inspect.getargspec(method).args[0] == 'self':
    # regular method. *Probably*

This of course fails entirely for static methods and nested functions that happen to have self as a first argument, as well as regular methods that do not use self as a first argument (flying in the face of convention).

For static methods and class methods, you'd have to look at the class dictionary instead:

>>> isinstance(vars(C)['st'], staticmethod)
True

That's because C.__dict__['st'] is the actual staticmethod instance, before binding to the class.

Solution 2:

Could you use inspect.isroutine(...)? Running it with your class C I get:

>>> inspect.isroutine(C.st)
True>>> inspect.isroutine(C.me)
True>>> inspect.isroutine(obj.st)
True>>> inspect.isroutine(obj.me)
True

Combining the results of inspect.isroutine(...) with the results of inspect.ismethod(...) may enable you to infer what you need to know.

Edit: dm03514's answer suggests you might also try inspect.isfunction():

>>> inspect.isfunction(obj.me)
False>>> inspect.isfunction(obj.st)
True>>> inspect.isfunction(C.st)
True>>> inspect.isfunction(C.me)
False

Though as Hernan has pointed out, the results of inspect.isfunction(...) change in python 3.

Solution 3:

Since inspect.ismethod returns True for both bound and unbound methods in Python 2.7 (ie., is broken), I'm using:

defis_bound_method(obj):
    returnhasattr(obj, '__self__') and obj.__self__ isnotNone

It 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 in Python 3, inspect.ismethod properly distinguishes between bound and unbound methods, so it is not necessary.

Post a Comment for "Detecting Bound Method In Classes (not Instances) In Python 3"