談?wù)凱ython:為什么類中的私有屬性可以在外部賦值并訪問(wèn)
Python:為什么類中的私有屬性可以在外部賦值并訪問(wèn)?
問(wèn)題引入
在慕課網(wǎng)上學(xué)習(xí)Python**類中的私有屬性**的時(shí)候,看到了一個(gè)同學(xué)的提問(wèn):
將count改為_(kāi)_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
問(wèn)題解決:
單刀直入版:
這是因?yàn)榻op1.__count賦值的操作,其實(shí)是在p1中定義了一個(gè)名為_(kāi)_count的變量(因?yàn)镻ython中的都是動(dòng)態(tài)變量),而沒(méi)有改變類中真正的屬性。
太長(zhǎng)但還是要看看版:
知識(shí)點(diǎn)清單:
1、類的“偽私有屬性”
2、在類的外部動(dòng)態(tài)地創(chuàng)建類屬性
問(wèn)題解決過(guò)程:
1、“偽私有屬性”的概念:
python的類中通過(guò)加雙下劃線來(lái)設(shè)置的“私有屬性”其實(shí)是“偽私有屬性”,原理是python編譯器將加了雙下劃線的“屬性名”自動(dòng)轉(zhuǎn)換成“類名屬性名”。所以我們?cè)谕獠坑谩皩傩悦痹L問(wèn)私有屬性的時(shí)候,會(huì)觸發(fā)AttributeError,從而實(shí)現(xiàn)“私有屬性”的特性。但通過(guò)“類名屬性名”也可以訪問(wè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í)例p1 p1 = 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_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 = original count_of_class Person's __count_of_class = I'm not the original count_of_class!
**由此可見(jiàn),雖然用p1.__count_of_class給它賦值了,但其實(shí)在類中真正的屬性_Person__count_of_class的原始值是沒(méi)有改變的。
但是如果將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è)東西。
最后的問(wèn)題
但是呢,如果不先給p1.__count_of_class賦值,直接打印它又會(huì)觸發(fā)AttributeError,這是為什么?
這是因?yàn)榻op1.__count_of_class賦值的操作,其實(shí)是在p1中定義了一個(gè)名為_(kāi)_count_of_class的變量(因?yàn)镻ython中的都是動(dòng)態(tài)變量)。
以下實(shí)例說(shuō)明可以通過(guò)外部賦值來(lái)為類創(chuàng)造屬性:
class Person(object): pass p1=Person() #給p1創(chuàng)建屬性new_of_instance p1.new_of_instance = 'I\'m new in p1!' print(p1.new_of_instance) #給Person類創(chuàng)建屬性new_of_class Person.new_of_class = 'I\'m new in Person!' #在類中新加的屬性,可以通過(guò)實(shí)例來(lái)訪問(wèn) print(p1.new_of_class) >>>輸出: I'm new in p1! I'm new in Person!
問(wèn)題解決。
以上這篇談?wù)凱ython:為什么類中的私有屬性可以在外部賦值并訪問(wèn)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Python 讀取用戶指令和格式化打印實(shí)現(xiàn)解析
- 如何安裝并使用conda指令管理python環(huán)境
- python執(zhí)行CMD指令,并獲取返回的方法
- Python3之外部文件調(diào)用Django程序操作model等文件實(shí)現(xiàn)方式
- Python如何調(diào)用外部系統(tǒng)命令
- 詳解python函數(shù)的閉包問(wèn)題(內(nèi)部函數(shù)與外部函數(shù)詳述)
- python 函數(shù)內(nèi)部修改外部變量的方法
- python3 打開(kāi)外部程序及關(guān)閉的示例
- python執(zhí)行外部程序的常用方法小結(jié)
- Python常用外部指令執(zhí)行代碼實(shí)例
相關(guān)文章
Python實(shí)現(xiàn)無(wú)損放大圖片的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用Python語(yǔ)言實(shí)現(xiàn)一個(gè)簡(jiǎn)單的無(wú)損放大圖片小程序,可以支持將JPG/PNG圖片無(wú)損放大上萬(wàn)像素,感興趣的可以了解一下2022-08-08基于python實(shí)現(xiàn)名片管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了基于python實(shí)現(xiàn)名片管理系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11利用Python實(shí)現(xiàn)翻譯HTML中的文本字符串
這篇文章主要為大家介紹了如何利用Python實(shí)現(xiàn)翻譯HTML中的文本字符串功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以動(dòng)手嘗試一下2022-06-06使用python實(shí)現(xiàn)3D聚類圖示例代碼
這篇文章主要介紹了使用python實(shí)現(xiàn)3D聚類圖效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-08-08Python subprocess庫(kù)六個(gè)實(shí)例快速掌握
這次來(lái)說(shuō)Python的第三方庫(kù)subprocess庫(kù),在python2.4以上的版本commands模塊被subprocess取代了。一般當(dāng)我們?cè)谟肞ython寫運(yùn)維腳本時(shí),需要履行一些Linux shell的命令,Python中subprocess模塊就是專門用于調(diào)用Linux shell命令,并返回狀態(tài)和結(jié)果,可以完美的解決這個(gè)問(wèn)題2022-10-10Python圖像處理庫(kù)Pillow的簡(jiǎn)單實(shí)現(xiàn)
本文主要介紹了Python圖像處理庫(kù)Pillow的簡(jiǎn)單實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06Python3.x檢查內(nèi)存可用大小的兩種實(shí)現(xiàn)
本文將介紹如何使用Python 3實(shí)現(xiàn)檢查L(zhǎng)inux服務(wù)器內(nèi)存可用大小的方法,包括使用Python標(biāo)準(zhǔn)庫(kù)實(shí)現(xiàn)和使用Linux命令實(shí)現(xiàn)兩種方式,感興趣可以了解一下2023-05-05Python的另外幾種語(yǔ)言實(shí)現(xiàn)
這篇文章主要介紹了Python的另外幾種語(yǔ)言實(shí)現(xiàn),本文介紹了CPython、Jython、Python for .NET、PyPy、Stackless等其它幾種語(yǔ)言實(shí)現(xiàn)的Python,需要的朋友可以參考下2015-01-01