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

标签 python python-datamodel

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

最佳答案

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.

在 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.

关于python - 你如何检查一个python方法是否被绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53225/

相关文章:

python - 如何计算 pandas groupby 中的所有正值和负值?

python - CNTK(微软认知工具包)导入错误

Python - 翻转文件中字节的有效方法?

python - 您将如何确定 Python 类的每个属性和方法的定义位置?

python - isinstance(type, object) = True,为什么?

python - `operator.__inv__` 存在的目的是什么?

python - 将数据框中的列转换为百分位排名 - Python 3.x

python - 使用 psycopg2 批量插入、返回和更新

Python 数据模型文档 : an unbound user-defined method object and a class method object

python - 自定义删除器方法的示例