How do you check whether a python method is bound

2019-01-23 02:02发布

Given a reference to a method, is there a way to check whether the method is bound to an object or not? Can you also access the instance that it's bound to?

4条回答
成全新的幸福
2楼-- · 2019-01-23 02:24

im_self attribute (only Python 2)

查看更多
在下西门庆
3楼-- · 2019-01-23 02:29

In python 3 the __self__ attribute is only set on bound methods. It's not set to None on plain functions (or unbound methods, which are just plain functions in python 3).

Use something like this:

def is_bound(m):
    return hasattr(m, '__self__')
查看更多
叛逆
4楼-- · 2019-01-23 02:30

The chosen answer is valid in almost all cases. However when checking if a method is bound in a decorator using chosen answer, the check will fail. Consider this example decorator and method:

def my_decorator(*decorator_args, **decorator_kwargs):
    def decorate(f):
        print(hasattr(f, '__self__'))
        @wraps(f)
        def wrap(*args, **kwargs):
            return f(*args, **kwargs)
        return wrap
    return decorate

class test_class(object):
    @my_decorator()
    def test_method(self, *some_params):
        pass

The print statement in decorator will print False. In this case I can't find any other way but to check function parameters using their argument names and look for one named self. This is also not guarantied to work flawlessly because the first argument of a method is not forced to be named self and can have any other name.

import inspect

def is_bounded(function):
    params = inspect.signature(function).parameters
    return params.get('self', None) is not None
查看更多
别忘想泡老子
5楼-- · 2019-01-23 02:32
def isbound(method):
    return method.im_self is not None

def instance(bounded_method):
    return bounded_method.im_self

User-defined methods:

When a user-defined method object is created by retrieving a user-defined function object from a class, its im_self attribute is None and the method object is said to be unbound. When one is created by retrieving a user-defined function object from a class via one of its instances, its im_self attribute is the instance, and the method object is said to be bound. In either case, the new method's im_class attribute is the class from which the retrieval takes place, and its im_func attribute is the original function object.

In Python 2.6 and 3.0:

Instance method objects have new attributes for the object and function comprising the method; the new synonym for im_self is __self__, and im_func is also available as __func__. The old names are still supported in Python 2.6, but are gone in 3.0.

查看更多
登录 后发表回答