Python類和實例的屬性機制原理詳解
實例是具象化的類,它可以作為類訪問所有靜態(tài)綁定到類上的屬性,包括類變量與方法,也可以作為實例訪問動態(tài)綁定到實例上的屬性。
實例1:
class A:
work = list("hello")
kind = list("world")
another = 1
def test1(self):
print(self.work, self.kind, self.another)
self.work[0], self.kind [0] = "t", "t"
self.another += 1
print(A.work, A.kind, A.another)
if __name__ == "__main__":
a = A()
a.test1()
輸出結(jié)果:
['h', 'e', 'l', 'l', 'o'] ['w', 'o', 'r', 'l', 'd'] 1
['t', 'e', 'l', 'l', 'o'] ['t', 'o', 'r', 'l', 'd'] 1
test1中演示了實例對類變量的訪問與修改,從輸出結(jié)果可以看到,類變量work和kind的列表被修改了,而another的值沒有發(fā)生變化,說明如果類變量是可變的,那么可以通過實例來對類變量進行修改,如果類變量不可變,那么實例無法修改類變量。
實例2:
class A:
work = list("hello")
kind = list("world")
another = 1
def test2(self):
A.work, A.kind = "hello", " world"
A.another += 2
print(self.__dict__)
print(self.work, self.kind, self.another)
A.test2 = 13
print(self.test2)
if __name__ == "__main__":
a = A()
a.test2()
輸出結(jié)果:
{'another': 2}
hello world 2
13
test2說明了實例訪問類變量與方法的機制,在test1中,已經(jīng)給實例動態(tài)綁定了一個another的屬性,值為2(因為有賦值語句)。在self.__dict__中可以看到確實出現(xiàn)了實例屬性another。
在使用實例訪問屬性(變量與方法)時,如果在實例的屬性集里沒有找到對應的屬性,那么就會到類的屬性集里找對應的屬性。self.work和self.kind和類變量保持一致,說明并沒有事先在實例與類變量之間建立引用,而是動態(tài)查找的。
class A:
work = list("hello")
kind = list("world")
another = 1
def test3(self):
print(self.__dict__)
self.w, self.k = 0, 1
print(self.__dict__)
self.work, self.kind = 4, 4
print(self.__dict__)
self.test1 = 12
print(self.__dict__)
try:
self.test1()
except:
print("test1 is not a bound method")
if __name__ == "__main__":
a = A()
a.test3()
輸出結(jié)果:
{'another': 2}
{'another': 2, 'w': 0, 'k': 1}
{'another': 2, 'w': 0, 'k': 1, 'work': 4, 'kind': 4}
{'another': 2, 'w': 0, 'k': 1, 'work': 4, 'kind': 4, 'test1': 12}
test1 is not a bound method
self.__dict__中保存了動態(tài)綁定到實例的變量與方法,只要出現(xiàn)了賦值語句,都是動態(tài)綁定屬性。如果動態(tài)綁定的屬性與類的變量或方法同名,在查找過程中就會覆蓋類的變量和方法。
總結(jié)
1. 動態(tài)綁定到實例的屬性位于self.__dict__中
2. 出現(xiàn)self.attribute = XXX之類的賦值語句都是在往實例上動態(tài)綁定屬性
3. 實例查找屬性的流程:self.work -> self.__dict__["work"] or cls.work,這是一個動態(tài)的過程,實例中的同名屬性會覆蓋類變量或方法,類變量或方法的修改會實時影響實例查找屬性的結(jié)果
4. 如果類變量是可修改的,如列表,字典等,可以通過實例來修改類變量,方法是不可修改的,故無法通過實例修改方法
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Python實現(xiàn)將一個正整數(shù)分解質(zhì)因數(shù)的方法分析
這篇文章主要介紹了Python實現(xiàn)將一個正整數(shù)分解質(zhì)因數(shù)的方法,結(jié)合實例形式對比分析了Python計算正整數(shù)分解質(zhì)因數(shù)的算法逐步改進操作技巧,需要的朋友可以參考下2017-12-12
Python基于gevent實現(xiàn)文件字符串查找器
這篇文章主要介紹了Python基于gevent實現(xiàn)文件字符串查找器,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-08-08
python中playwright結(jié)合pytest執(zhí)行用例的實現(xiàn)
本文主要介紹了python中playwright結(jié)合pytest執(zhí)行用例的實現(xiàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12
詳解Python prometheus_client使用方式
本文主要介紹了Python prometheus_client使用方式,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02

