python類的私有屬性和公共屬性說(shuō)明
python類私有屬性和公共屬性
對(duì)于python而言,類的屬性的可見(jiàn)度只有兩種,public和private。
類的私有屬性便是在前面加上“__”標(biāo)識(shí)符,而公共屬性則不必。
在類的外面訪問(wèn)私有屬性會(huì)引發(fā)異常。
class Base: ? ? def __init__(self, value): ? ? ? ? self.__value = value ? b = Base(5) print(assert b.__value) ? Traceback (most recent call last): ? File "/Users/yangjiajia/Desktop/project/python/algebra/test.py", line 19, in <module> ? ? print(b.__value) AttributeError: 'Base' object has no attribute '__value'
屬性被私有化,即使繼承他的字類也不能訪問(wèn)到。
class Parent: ? ? def __init__(self, value): ? ? ? ? self.__value = value? ? class Child(Parent): ? ? def get_value(self): ? ? ? ? return self.__value ? child = Child(4) print(child.get_value()) ? Traceback (most recent call last): ? File "/Users/yangjiajia/Desktop/project/python/algebra/test.py", line 24, in <module> ? ? print(child.get_value()) ? File "/Users/yangjiajia/Desktop/project/python/algebra/test.py", line 21, in get_value ? ? return self.__value AttributeError: 'Child' object has no attribute '_Child__value'
為何會(huì)這樣?因?yàn)閜ython會(huì)對(duì)類的私有屬性做些轉(zhuǎn)換,以保證private字段的私密性。當(dāng)編譯器看到Child.get_value方法要訪問(wèn)私有屬性時(shí),他會(huì)先把__value變換為_(kāi)Child_value然后再進(jìn)行訪問(wèn),但實(shí)際上該私有屬性是_Parent__value。字類無(wú)法訪問(wèn)父類的私有屬性,只是因?yàn)樵L問(wèn)的名稱不同。
查詢?cè)搶?duì)象的屬性字典便知
class Parent:
? ? def __init__(self, value):
? ? ? ? self.__value = value?
?
class Child(Parent):
? ? def name(self):
? ? ? ? names = 'yang'
?
? ? def get_value(self):
? ? ? ? return self.__value
?
child = Child(4)
print(child.__dict__)
?
{'_Parent__value': 4}python開(kāi)發(fā)的原則還是要少用私有屬性,如果需要保證屬性不重復(fù),可以在其前面加上單個(gè)下劃線。
class Parent: ? ? def __init__(self, value): ? ? ? ? self._value = value? ? class Child(Parent): ? ? def get_value(self): ? ? ? ? return self._value ? child = Child(4) assert child._value == 4
python私有屬性的定義
在Java中用private對(duì)變量或者方法進(jìn)行限定,則方法僅在整個(gè)類內(nèi)部可見(jiàn),外部不可見(jiàn)、不可訪問(wèn)。
在python中也有私有屬性定義,使用函數(shù)名前綴的兩個(gè)下劃線進(jìn)行定義。
例如 __parameter
接下來(lái)看上一個(gè)Dog的例子
class Dog(object):
? ? __slots__ = ('__name', 'kind', 'level')
? ? def __init__(self,name,kind,level):#構(gòu)造函數(shù),定義屬性和初始方法
? ? ? ? self.__name=name
? ? ? ? self.kind=kind
? ? ? ? self.level=level#賦值
? ? ? ? print(f"This is a {self.kind} dog called {self.__name} with level {self.level}")
?
? ? def run(self):#定義類中的方法
? ? ? ? print(f"{self.__name} is now running!")
?
? ? def roll_over(self):
? ? ? ? print(f"{self.__name} is now rolling over!")
?
? ? def change_level(self):
? ? ? ? self.level+=1#修改類中屬性的值
? ? ? ? print(f"The level of {self.__name} is now {self.level}")在另一個(gè)文件里面進(jìn)行調(diào)用
from Zoo.Dog import Dog#導(dǎo)包
?
dog=Dog("Halo","Husty",3)#實(shí)例化
?
dog.run()
dog.roll_over()
dog.change_level()#調(diào)用方法發(fā)現(xiàn)當(dāng)我們輸入以下內(nèi)容時(shí),會(huì)報(bào)錯(cuò),非法訪問(wèn)。
print(dog.__name)
在本例中,__name是對(duì)name進(jìn)行私有限定。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python發(fā)送郵件示例(支持中文郵件標(biāo)題)
python發(fā)送中文郵件示例,支持中文郵件標(biāo)題和中文郵件內(nèi)容。支持多附件。根據(jù)用戶名推測(cè)郵件服務(wù)器提供商2014-02-02
CentOS 6.5中安裝Python 3.6.2的方法步驟
centos 6.5默認(rèn)自帶的python版本為2.6,而下面這篇文章主要給大家介紹了關(guān)于在CentOS 6.5中安裝Python 3.6.2的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12
python爬蟲(chóng)beautifulsoup庫(kù)使用操作教程全解(python爬蟲(chóng)基礎(chǔ)入門)
這篇文章主要介紹了python爬蟲(chóng)beautifulsoup庫(kù)使用操作全解(python爬蟲(chóng)基礎(chǔ)入門),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
Python PaddleNLP實(shí)現(xiàn)自動(dòng)生成虎年藏頭詩(shī)
這篇文章主要介紹了利用Python PaddleNLP實(shí)現(xiàn)自動(dòng)生成虎年藏頭詩(shī)功能,文中的示例代碼講解詳細(xì),感興趣的同學(xué)可以跟隨小編一起試一試2022-01-01

