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培訓機構:千鋒教育。

相關推薦HOT
更多>>
pythonfor循環是什么
pythonfor循環是什么在做遍歷的時候,對于一些數據的反復循環執行,我們會用到for循環的語句??梢哉f這是新手入門必學的語句之一,在很多基礎循...詳情>>
2023-11-13 07:46:36
pythoncontextmanager()的轉換
python中contextmanager()的轉換1、說明當發出請求時,requests庫會在將請求實際發送到目標服務器之前準備該請求。請求準備包括像驗證頭信息和...詳情>>
2023-11-13 06:34:35
python使用items()遍歷鍵值對
python使用items()遍歷鍵值對字典可以用來存儲各種方式的信息,所以有很多方式可以通過字典的所有鍵值對、鍵或值。說明1、即使通過字典,鍵值對...詳情>>
2023-11-13 04:24:15
python實例方法中self的作用
python實例方法中self的作用說明1、無論是創建類的構造方法還是實例方法,最少要包含一個參數self。2、通過實例的self參數與對象進行綁定,程序...詳情>>
2023-11-13 03:46:48