国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

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

談談Python:為什么類中的私有屬性可以在外部賦值并訪問

瀏覽:6日期:2022-08-04 08:49:42

Python:為什么類中的私有屬性可以在外部賦值并訪問?

問題引入

在慕課網上學習Python**類中的私有屬性**的時候,看到了一個同學的提問:

將count改為__count,為什么實例變量在外部仍然可以修改__count?這里print p1.__count可以打印出100

class Person(object): __count = 0 def __init__(self, name): Person.__count = Person.__count + 1 self.name = name print Person.__count p1 = Person(’Bob’) p1.__count=100 print p1.__count p2 = Person(’Alice’)print Person.__count

問題解決:

單刀直入版:

這是因為給p1.__count賦值的操作,其實是在p1中定義了一個名為__count的變量(因為Python中的都是動態變量),而沒有改變類中真正的屬性。

太長但還是要看看版:

知識點清單:

1、類的“偽私有屬性” 2、在類的外部動態地創建類屬性

問題解決過程:

1、“偽私有屬性”的概念:

python的類中通過加雙下劃線來設置的“私有屬性”其實是“偽私有屬性”,原理是python編譯器將加了雙下劃線的“屬性名”自動轉換成“類名屬性名”。所以我們在外部用“屬性名”訪問私有屬性的時候,會觸發AttributeError,從而實現“私有屬性”的特性。但通過“類名屬性名”也可以訪問這些屬性。

參考:http://www.pythonclub.org/python-class/private

2、編寫測試代碼:

以下是在該同學的代碼的基礎上修改的測試代碼:

class Person(object): #設置類屬性 __count_of_class = ’original count_of_class’ def __init__(self, name): self.name = name print(’in class Person : count_of_class = ’, Person.__count_of_class,’n’)#初始化實例p1p1 = Person(’Bob’)#在實例p1上修改屬性值p1.__count_of_class=’I’m not the original count_of_class!’print(’p1’s _Person__count_of_class = ’,p1._Person__count_of_class)print(’p1’s __count_of_class = ’,p1.__count_of_class,’n’)#在類Person上修改屬性值Person.__count_of_class = ’I’m not the original count_of_class!’#將這句注釋取消掉,會發現真正的私有屬性的值也改變了#Person._Person__count_of_class = ’I’m not the original count_of_class!’print(’Person’s _Person__count_of_class = ’,Person._Person__count_of_class)print(’Person’s __count_of_class = ’,Person.__count_of_class)

分別在實例p1上和類Person上進行操作,并且分別打印出“__屬性名”,以及“_類名__屬性名”。

輸出結果如下:

in class Person : count_of_class = original count_of_classp1’s _Person__count_of_class = original count_of_classp1’s __count_of_class = I’m not the original count_of_class!Person’s _Person__count_of_class = original count_of_classPerson’s __count_of_class = I’m not the original count_of_class!

**由此可見,雖然用p1.__count_of_class給它賦值了,但其實在類中真正的屬性_Person__count_of_class的原始值是沒有改變的。

但是如果將p1._Person__count_of_class賦值,那么類屬性定義的原始值就真正地被覆蓋了**

'''取消掉##Person._Person__count_of_class = ’I’m not the original count_of_class!’的注釋,輸出結果:'''in class Person : count_of_class = original count_of_class p1’s _Person__count_of_class = original count_of_class p1’s __count_of_class = I’m not the original count_of_class! #注意這一句:Person’s _Person__count_of_class = I’m not the original count_of_class! Person’s __count_of_class = I’m not the original count_of_class!

由此,我們知道了:_count_of_class和_Person_count_of_class不是同一個東西。

最后的問題

但是呢,如果不先給p1.__count_of_class賦值,直接打印它又會觸發AttributeError,這是為什么?

這是因為給p1.__count_of_class賦值的操作,其實是在p1中定義了一個名為__count_of_class的變量(因為Python中的都是動態變量)。

以下實例說明可以通過外部賦值來為類創造屬性:

class Person(object): passp1=Person()#給p1創建屬性new_of_instancep1.new_of_instance = ’I’m new in p1!’print(p1.new_of_instance)#給Person類創建屬性new_of_classPerson.new_of_class = ’I’m new in Person!’#在類中新加的屬性,可以通過實例來訪問print(p1.new_of_class)>>>輸出:I’m new in p1!I’m new in Person!

問題解決。

以上這篇談談Python:為什么類中的私有屬性可以在外部賦值并訪問就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 依依成人综合网 | a级成人高清毛片 | 99视频在线播放 | 一级片 mp4| 日本高清免费视频www | 欧美一区二区三区在线观看免费 | 亚洲国产精品a在线 | a级片在线观看视频 | 久久影院视频 | 久久免费黄色 | 国产乱子伦露脸对白在线小说 | 一级黄色欧美片 | 很黄很色的摸下面的视频 | 亚洲手机看片 | 日本aaa毛片 | 91热久久免费频精品黑人99 | 国产精品激情丝袜美女 | 老司机亚洲精品影院 | 国产亚洲女在线精品 | 日本三级香港三级乳网址 | 步兵社区 | 中国欧美一级毛片免费 | 免费在线观看的毛片 | 美女视频免费看视频网站 | 美女黄色免费在线观看 | 性欧美另类老妇高清 | 国产三级日本三级日产三 | 国产成人免费高清视频 | 国产成a人亚洲精v品久久网 | 亚洲综合色在线观看 | 国产精品爱久久久久久久三级 | 久久久久久久久影院 | 亚洲国产精品成 | 伊大人香蕉久久网欧美 | 日韩毛片免费在线观看 | 成年男人午夜片免费观看 | 精品九九久久国内精品 | 亚洲一区二区三区四区在线观看 | 亚洲午夜色| 欧洲美女与男人做爰 | 国产夫妇肉麻对白 |