python 你如何检查一个python方法是否被绑定?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 19:27:44  来源:igfitidea点击:

How do you check whether a python method is bound or not?

pythonpython-datamodel

提问by Readonly

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?

给定一个方法的引用,有没有办法检查该方法是否绑定到一个对象?您还可以访问它绑定到的实例吗?

回答by jfs

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_selfattribute is Noneand 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_selfattribute is the instance, and the method object is said to be bound. In either case, the new method's im_classattribute is the class from which the retrieval takes place, and its im_funcattribute is the original function object.

当通过从类中检索用户定义的函数对象来创建用户定义的方法对象时,它的 im_self属性是None,并且方法对象被称为未绑定。当通过其实例之一从类中检索用户定义的函数对象来创建一个时,它的 im_self属性是实例,并且方法对象被称为绑定。无论哪种情况,新方法的 im_class属性都是从中进行检索的类,其im_func属性是原始函数对象。

In Python 2.6 and 3.0:

在 Python 2.6 和 3.0 中

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

实例方法对象具有包含该方法的对象和函数的新属性;的新同义词im_self__self__im_func也可用作__func__。Python 2.6 中仍支持旧名称,但在 3.0 中已消失。

回答by Rob Agar

In python 3 the __self__attribute is onlyset on bound methods. It's not set to Noneon plain functions (or unbound methods, which are just plain functions in python 3).

在 python 3 中,该__self__属性在绑定方法上设置。它没有设置为None普通函数(或未绑定方法,它们只是 python 3 中的普通函数)。

Use something like this:

使用这样的东西:

def is_bound(m):
    return hasattr(m, '__self__')

回答by Klaus

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 printstatement 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 notguarantied to work flawlessly because the first argument of a method is not forced to be named selfand can have any other name.

print装饰器中的语句将打印False。在这种情况下,我找不到任何其他方法,只能使用参数名称检查函数参数并查找名为self. 这也不能保证完美地工作,因为方法的第一个参数不是强制命名的self,可以有任何其他名称。

import inspect

def is_bounded(function):
    params = inspect.signature(function).parameters
    return params.get('self', None) is not None

回答by Mark Cidade

im_selfattribute(only Python 2)

im_self属性(仅限 Python 2)