欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2020年03月05日 09:24:54   作者:理Risen  
這篇文章主要介紹了談談Python:為什么類中的私有屬性可以在外部賦值并訪問,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

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

問題引入

在慕課網(wǎng)上學習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中的都是動態(tài)變量),而沒有改變類中真正的屬性。

太長但還是要看看版:

知識點清單:

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

問題解決過程:

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

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

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

2、編寫測試代碼:

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

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

#初始化實例p1
p1 = 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!'
#將這句注釋取消掉,會發(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)


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

輸出結(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!

**由此可見,雖然用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!'
的注釋,輸出結(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不是同一個東西。

最后的問題

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

這是因為給p1.__count_of_class賦值的操作,其實是在p1中定義了一個名為__count_of_class的變量(因為Python中的都是動態(tà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!'

#在類中新加的屬性,可以通過實例來訪問
print(p1.new_of_class)


>>>輸出:
I'm new in p1!
I'm new in Person!

問題解決。

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

相關(guān)文章

  • Python實現(xiàn)無損放大圖片的示例代碼

    Python實現(xiàn)無損放大圖片的示例代碼

    這篇文章主要為大家詳細介紹了如何利用Python語言實現(xiàn)一個簡單的無損放大圖片小程序,可以支持將JPG/PNG圖片無損放大上萬像素,感興趣的可以了解一下
    2022-08-08
  • 基于python實現(xiàn)名片管理系統(tǒng)

    基于python實現(xiàn)名片管理系統(tǒng)

    這篇文章主要為大家詳細介紹了基于python實現(xiàn)名片管理系統(tǒng),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • 利用Python實現(xiàn)翻譯HTML中的文本字符串

    利用Python實現(xiàn)翻譯HTML中的文本字符串

    這篇文章主要為大家介紹了如何利用Python實現(xiàn)翻譯HTML中的文本字符串功能,文中的示例代碼講解詳細,感興趣的小伙伴可以動手嘗試一下
    2022-06-06
  • 使用python實現(xiàn)3D聚類圖示例代碼

    使用python實現(xiàn)3D聚類圖示例代碼

    這篇文章主要介紹了使用python實現(xiàn)3D聚類圖效果,本文通過實例代碼給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧
    2024-08-08
  • Python subprocess庫六個實例快速掌握

    Python subprocess庫六個實例快速掌握

    這次來說Python的第三方庫subprocess庫,在python2.4以上的版本commands模塊被subprocess取代了。一般當我們在用Python寫運維腳本時,需要履行一些Linux shell的命令,Python中subprocess模塊就是專門用于調(diào)用Linux shell命令,并返回狀態(tài)和結(jié)果,可以完美的解決這個問題
    2022-10-10
  • 解讀Python中的frame是什么

    解讀Python中的frame是什么

    這篇文章主要介紹了解讀Python中的frame是什么,關(guān)于frame使用講解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • Python圖像處理庫Pillow的簡單實現(xiàn)

    Python圖像處理庫Pillow的簡單實現(xiàn)

    本文主要介紹了Python圖像處理庫Pillow的簡單實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-06-06
  • 通俗易懂了解Python裝飾器原理

    通俗易懂了解Python裝飾器原理

    這篇文章主要介紹了通俗易懂了解Python裝飾器原理,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-09-09
  • Python3.x檢查內(nèi)存可用大小的兩種實現(xiàn)

    Python3.x檢查內(nèi)存可用大小的兩種實現(xiàn)

    本文將介紹如何使用Python 3實現(xiàn)檢查Linux服務器內(nèi)存可用大小的方法,包括使用Python標準庫實現(xiàn)和使用Linux命令實現(xiàn)兩種方式,感興趣可以了解一下
    2023-05-05
  • Python的另外幾種語言實現(xiàn)

    Python的另外幾種語言實現(xiàn)

    這篇文章主要介紹了Python的另外幾種語言實現(xiàn),本文介紹了CPython、Jython、Python for .NET、PyPy、Stackless等其它幾種語言實現(xiàn)的Python,需要的朋友可以參考下
    2015-01-01

最新評論