python3.x - Python 3.x 实例方法的__func__属性

查看:441
本文介绍了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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆