国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

Python類和實例的屬性機制原理詳解

瀏覽:36日期:2022-08-01 16:13:09

實例是具象化的類,它可以作為類訪問所有靜態綁定到類上的屬性,包括類變量與方法,也可以作為實例訪問動態綁定到實例上的屬性。

實例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()

輸出結果:

[’h’, ’e’, ’l’, ’l’, ’o’] [’w’, ’o’, ’r’, ’l’, ’d’] 1[’t’, ’e’, ’l’, ’l’, ’o’] [’t’, ’o’, ’r’, ’l’, ’d’] 1

test1中演示了實例對類變量的訪問與修改,從輸出結果可以看到,類變量work和kind的列表被修改了,而another的值沒有發生變化,說明如果類變量是可變的,那么可以通過實例來對類變量進行修改,如果類變量不可變,那么實例無法修改類變量。

實例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()

輸出結果:

{’another’: 2} hello world 2 13

test2說明了實例訪問類變量與方法的機制,在test1中,已經給實例動態綁定了一個another的屬性,值為2(因為有賦值語句)。在self.__dict__中可以看到確實出現了實例屬性another。

在使用實例訪問屬性(變量與方法)時,如果在實例的屬性集里沒有找到對應的屬性,那么就會到類的屬性集里找對應的屬性。self.work和self.kind和類變量保持一致,說明并沒有事先在實例與類變量之間建立引用,而是動態查找的。

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()

輸出結果:

{’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__中保存了動態綁定到實例的變量與方法,只要出現了賦值語句,都是動態綁定屬性。如果動態綁定的屬性與類的變量或方法同名,在查找過程中就會覆蓋類的變量和方法。

總結

1. 動態綁定到實例的屬性位于self.__dict__中

2. 出現self.attribute = XXX之類的賦值語句都是在往實例上動態綁定屬性

3. 實例查找屬性的流程:self.work -> self.__dict__['work'] or cls.work,這是一個動態的過程,實例中的同名屬性會覆蓋類變量或方法,類變量或方法的修改會實時影響實例查找屬性的結果

4. 如果類變量是可修改的,如列表,字典等,可以通過實例來修改類變量,方法是不可修改的,故無法通過實例修改方法

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 黑河市| 万全县| 樟树市| 苗栗市| 佛山市| 清丰县| 老河口市| 桃园市| 邯郸市| 长白| 万全县| 博客| 军事| 胶州市| 常州市| 景东| 南部县| 汉阴县| 武邑县| 白水县| 英吉沙县| 西林县| 沐川县| 清原| 夏河县| 上饶市| 新田县| 扶风县| 定西市| 襄樊市| 炎陵县| 亳州市| 伊宁市| 普兰县| 浦北县| 泰来县| 浑源县| 庆元县| 民乐县| 定结县| 成武县|