看 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
的创建方式的描述到底有何不同? 还请大神赐教
诶,谢邀。这段话确实难懂。
先摆一段代码:
明确一个概念:
A().foo
即为第一种方法创建的 method,A().bar
则为第二种。那段话的意思是:A().bar.__func__
不是指向A().foo
,而是指向A().foo.__func__
。