python中getattribute方法作用是什么?

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

getattribute介绍

1、python中内建属性;

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

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

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

使用实例

classMyClass:
def__init__(self,x):
self.x=x
def__getattribute__(self,item):
print('正在获取属性{}'.format(item))
returnsuper(MyClass,self).__getattribute__(item)
>>>obj=MyClass(2)
>>>obj.x
正在获取属性x
2
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。