python中getattribute方法作用是什么?

python中getattribute方法作用是什么?,第1张

python中getattribute方法作用是什么?

本文教程 *** 作环境:windows7系统、Python 3.9.1,DELL G3电脑。

getattribute介绍

1、python中内建属性

2、__getattribute__是属性访问拦截器;

3、当类中属性被访问时,会自动调用__getattribute__方法;

4、常用于查看权限、打印log日志。

使用实例

class MyClass:

    def __init__(self, x):
        self.x = x

    def __getattribute__(self, item):
        print('正在获取属性{}'.format(item))
        return super(MyClass, self).__getattribute__(item)
>>> obj = MyClass(2)
>>> obj.x
正在获取属性x
2

以上就是python中getattribute方法的介绍,大家可以直接调用getattribute方法访问对象的属性值哦~

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/3013521.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-09-28
下一篇 2022-09-28

发表评论

登录后才能评论

评论列表(0条)

保存