python3.x - Python 3.x 实例方法的__func__属性
问题描述
看 Python 3.x 的文档,涉及到实例方法特殊属性的内容,有这么一段话描述 instance method
的 __func__
属性:
When an instance method object is created by retrieving a user-defined function object from a class via one of its instances, its self attribute is the instance, and the method object is said to be bound. The new method’s func attribute is the original function object.
When a user-defined method object is created by retrieving another method object from a class or instance, the behaviour is the same as for a function object, except that the func attribute of the new instance is not the original method object but its func attribute.
甚是不解,主要是不明白前后两段中对instance method object
的创建方式的描述到底有何不同? 还请大神赐教
诶,谢邀。这段话确实难懂。
先摆一段代码:
class A:
def foo(self): pass
bar = foo
明确一个概念:
method 是一种 function,method 有一个
__func__
属性,指向一个 function。
A().foo
即为第一种方法创建的 method,A().bar
则为第二种。那段话的意思是: A().bar.__func__
不是指向 A().foo
,而是指向 A().foo.__func__
。
这篇关于python3.x - Python 3.x 实例方法的__func__属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!