python的反射(自?。?就是自己檢查自己,自己操作自己的各個(gè)屬性值時(shí)各種返回狀態(tài)
python面向?qū)ο髮傩苑譃椋簩?shí)例屬性(變量)和方法屬性(函數(shù)方法,動(dòng)作)
#反射
# class School:
# name ="SB"
# def __init__(self,name):
# self.name1 = name
# def test(self):
# print("測試下反射")
# def __getattr__(self, item):#調(diào)用的屬性在類屬性或者實(shí)例屬性中不存在執(zhí)行
# print("測試下反射get")
# def __delattr__(self, item):#刪除實(shí)例的屬性時(shí)執(zhí)行,不存在不會報(bào)錯(cuò)
# print("測試下delattr反射")
# def __setattr__(self, key, value):#設(shè)置或者添加實(shí)例屬性時(shí)執(zhí)行
# print("測試下setattr反射")
#
# s = School("sb")
# print(s.__dict__)
#s.__dict__["tag"] = False
# print(hasattr(s,"name")) #查找類或者實(shí)例中的屬性,存在返回Ture 不存在返回Flase
# print(getattr(s,"name1111","屬性不存在"))#獲取實(shí)例或者類中的屬性,存在返回實(shí)例或者類的屬性值,不存在提示默認(rèn)設(shè)置的屬性
#或者報(bào)錯(cuò)
# setattr(s,"name","2")#設(shè)置、添加實(shí)例屬性值 如果實(shí)例的屬性存在則是設(shè)置操作。不存在則是添加操作
# #del s.name
#delattr(s,"name")#刪除實(shí)例屬性值,沒有則報(bào)AttributeError
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點(diǎn)擊舉報(bào)。