這篇文章給大家分享的是有關(guān)Python類和實(shí)例的屬性機(jī)制原理是什么的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

實(shí)例是具象化的類,它可以作為類訪問所有靜態(tài)綁定到類上的屬性,包括類變量與方法,也可以作為實(shí)例訪問動態(tài)綁定到實(shí)例上的屬性。
實(shí)例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中演示了實(shí)例對類變量的訪問與修改,從輸出結(jié)果可以看到,類變量work和kind的列表被修改了,而another的值沒有發(fā)生變化,說明如果類變量是可變的,那么可以通過實(shí)例來對類變量進(jìn)行修改,如果類變量不可變,那么實(shí)例無法修改類變量。
實(shí)例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說明了實(shí)例訪問類變量與方法的機(jī)制,在test1中,已經(jīng)給實(shí)例動態(tài)綁定了一個another的屬性,值為2(因為有賦值語句)。在self.__dict__中可以看到確實(shí)出現(xiàn)了實(shí)例屬性another。
在使用實(shí)例訪問屬性(變量與方法)時,如果在實(shí)例的屬性集里沒有找到對應(yīng)的屬性,那么就會到類的屬性集里找對應(yīng)的屬性。self.work和self.kind和類變量保持一致,說明并沒有事先在實(shí)例與類變量之間建立引用,而是動態(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)綁定到實(shí)例的變量與方法,只要出現(xiàn)了賦值語句,都是動態(tài)綁定屬性。如果動態(tài)綁定的屬性與類的變量或方法同名,在查找過程中就會覆蓋類的變量和方法。
總結(jié)
1. 動態(tài)綁定到實(shí)例的屬性位于self.__dict__中
2. 出現(xiàn)self.attribute = XXX之類的賦值語句都是在往實(shí)例上動態(tài)綁定屬性
3. 實(shí)例查找屬性的流程:self.work -> self.__dict__["work"] or cls.work,這是一個動態(tài)的過程,實(shí)例中的同名屬性會覆蓋類變量或方法,類變量或方法的修改會實(shí)時影響實(shí)例查找屬性的結(jié)果
4. 如果類變量是可修改的,如列表,字典等,可以通過實(shí)例來修改類變量,方法是不可修改的,故無法通過實(shí)例修改方法
感謝各位的閱讀!關(guān)于“Python類和實(shí)例的屬性機(jī)制原理是什么”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
分享名稱:Python類和實(shí)例的屬性機(jī)制原理是什么-創(chuàng)新互聯(lián)
文章轉(zhuǎn)載:http://www.chinadenli.net/article26/ipcjg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、App開發(fā)、網(wǎng)站營銷、網(wǎng)站導(dǎo)航、網(wǎng)站策劃、外貿(mào)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容