亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

談?wù)凱ython:為什么類中的私有屬性可以在外部賦值并訪問

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

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

問題引入

在慕課網(wǎng)上學(xué)習(xí)Python**類中的私有屬性**的時(shí)候,看到了一個(gè)同學(xué)的提問:

將count改為__count,為什么實(shí)例變量在外部仍然可以修改__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

問題解決:

單刀直入版:

這是因?yàn)榻op1.__count賦值的操作,其實(shí)是在p1中定義了一個(gè)名為__count的變量(因?yàn)镻ython中的都是動(dòng)態(tài)變量),而沒有改變類中真正的屬性。

太長但還是要看看版:

知識(shí)點(diǎn)清單:

1、類的“偽私有屬性” 2、在類的外部動(dòng)態(tài)地創(chuàng)建類屬性

問題解決過程:

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

python的類中通過加雙下劃線來設(shè)置的“私有屬性”其實(shí)是“偽私有屬性”,原理是python編譯器將加了雙下劃線的“屬性名”自動(dòng)轉(zhuǎn)換成“類名屬性名”。所以我們?cè)谕獠坑谩皩傩悦痹L問私有屬性的時(shí)候,會(huì)觸發(fā)AttributeError,從而實(shí)現(xiàn)“私有屬性”的特性。但通過“類名屬性名”也可以訪問這些屬性。

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

2、編寫測(cè)試代碼:

以下是在該同學(xué)的代碼的基礎(chǔ)上修改的測(cè)試代碼:

class Person(object): #設(shè)置類屬性 __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’)#初始化實(shí)例p1p1 = Person(’Bob’)#在實(shí)例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!’#將這句注釋取消掉,會(huì)發(fā)現(xiàn)真正的私有屬性的值也改變了#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)

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

輸出結(jié)果如下:

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給它賦值了,但其實(shí)在類中真正的屬性_Person__count_of_class的原始值是沒有改變的。

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

'''取消掉##Person._Person__count_of_class = ’I’m not the original count_of_class!’的注釋,輸出結(jié)果:'''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不是同一個(gè)東西。

最后的問題

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

這是因?yàn)榻op1.__count_of_class賦值的操作,其實(shí)是在p1中定義了一個(gè)名為__count_of_class的變量(因?yàn)镻ython中的都是動(dòng)態(tài)變量)。

以下實(shí)例說明可以通過外部賦值來為類創(chuàng)造屬性:

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

問題解決。

以上這篇談?wù)凱ython:為什么類中的私有屬性可以在外部賦值并訪問就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 91精品国产免费网站 | 不卡福利视频 | 在线观看福利视频在线观看 | 免费精品国产日韩热久久 | 密桃av| 九九老司机在线视频精品 | 国产精品久久久久久久福利院 | 97就要鲁就要鲁夜夜爽 | 国产一区二区高清视频 | 日韩大片观看网址 | 天天伊人网 | 黄色免费视屏 | 久久综合九色综合精品 | 女人洗澡一级毛片一级毛片 | 欧美笫一页 | 欧美一区二区三区播放 | 色综久久天天综合绕视看 | 色综合久久久久久中文网 | 久久久精品免费热线观看 | 欧美成人免费xxx大片 | 国产91精品一区二区视色 | 国产精品1区2区3区 国产精品1区2区3区在线播放 | 国产一在线精品一区在线观看 | 亚洲国产日韩欧美一区二区三区 | 欧美综合亚洲图片综合区 | 亚洲精品一区二 | 国产精品第一 | 欧美国产一区二区 | 9i9精品国产免费久久 | 久青草青综合在线视频 | 亚洲综合激情五月色播 | 成年美女黄网站色视频大全免费 | 久久国产精品高清一区二区三区 | 性感美女香蕉视频 | 特黄的欧美毛片 | 国产精品一区二区三区高清在线 | 久久永久免费视频 | 国产精品冒白浆免费视频 | 亚洲精品福利视频 | 亚洲小说春色综合另类网蜜桃 | 国产亚洲情侣一区二区无 |