python中getattribute方法作用是什么?

python中getattribute方法作用是什么?

本文教程操作环境:windows7系统、Python3.9.1,DELLG3电脑。

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

以上就是python中getattribute方法的介绍,大家可以直接调用getattribute方法访问对象的属性值哦~更多Python学习教程请关注IT培训机构:千锋教育。

申请14天超长免费试听资格
获取500G教程资料
姓名
电话
课程
立即申请