Python中的getter和setter的方法使用詳解
本文主要內(nèi)容:
- 解釋setter和getter的使用方法解釋@property裝飾器的妙用
- 在python中,setter和getter方法并不像其它編程語(yǔ)言中的那樣。基本上,在面向?qū)ο缶幊陶Z(yǔ)言中,使用setter和getter方法的主要目的是為了確保數(shù)據(jù)的封裝。不像其它面向?qū)ο缶幊陶Z(yǔ)言,python中的私有變量并不是真正的隱藏字段。在python中,通常在以下情況會(huì)用到setter和getter方法:
- 在獲取或者設(shè)置屬性值的時(shí)候使用setter和getter方法為其添加驗(yàn)證邏輯避免對(duì)類的某些字段直接訪問(wèn),比如類的私有變量不應(yīng)該被外部調(diào)用者直擊訪問(wèn)或者修改
使用普通函數(shù)實(shí)現(xiàn)setter和getter方法
要實(shí)現(xiàn)setter和getter屬性,只是定義普通方法get()和set()并不能反產(chǎn)生任何特殊的行為,例如:
class Student(object):
def __int(self, age=0):
self._age = age
# getter方法
def get(self):
return self._age
# setter方法
def set(self, value):
self._age = value
xiaoming = Student()
# 使用setter方法設(shè)置age
xiaoming.set(20)
# 使用getter方法返回age
print(xiaoming.get())
print(xiaoming._age)輸出:
20
20
在上面代碼中,set_age()和get_age()方法與普通方法并沒(méi)有什么兩樣,那么如何實(shí)現(xiàn)像getter和setter一樣的功能呢?這就要用到python中的特殊方法property()。
使用property()方法來(lái)實(shí)現(xiàn)setter和getter的行為
property()是python中的一個(gè)內(nèi)置方法,它創(chuàng)建并返回一個(gè)屬性對(duì)象。一個(gè)屬性對(duì)象有三個(gè)方法,getter()、setter()和delete()。property()內(nèi)置方法有四個(gè)參數(shù),property(fget,fset, fdel, doc)。fget是一個(gè)用于獲取屬性值的函數(shù),fset是一個(gè)用于設(shè)置屬性值的函數(shù),fdel是一個(gè)用于刪除屬性的函數(shù),doc用于為屬性創(chuàng)建文檔說(shuō)明。一個(gè)屬性兌現(xiàn)有三個(gè)方法,getter()、setter()和delete()分別制定fget、fset、fdel。
class Adult(object):
def __int(self):
self.__age = 0
# 獲取屬性_age的值
def get_age(self):
print('getter() method called')
return self.__age
# 設(shè)置屬性_age的值
def set_age(self, value):
print('setter() method called')
self.__age = value
# 刪除屬性_age
def del_age(self):
del self.__age
age = property(get_age, set_age, del_age)
laowang = Adult()
laowang.age = 60
print(laowang.age)輸出:
setter() method called
getter() method called
60
在上面的代碼中,age就是一個(gè)屬性對(duì)象,它保證了對(duì)私有變量的安全訪問(wèn)。
使用@property裝飾器來(lái)實(shí)現(xiàn)setter和getter的行為
除了上面使用property()的方法來(lái)實(shí)現(xiàn)getter、setter方法的行為,在python中還可以裝飾器@property來(lái)實(shí)現(xiàn)。@property是python的一個(gè)內(nèi)置裝飾器,使用裝飾器的目的是改變類的方法或者屬性,這樣調(diào)用者就無(wú)需在代碼中做任何改動(dòng)。
class Adult(object):
def __init__(self):
self.__age = 0
@property
def age(self):
print('getter() method called')
return self.__age
@age.setter
def age(self, value):
if value < 18:
raise ValueError('Sorry, you are a child, games not allowed')
print('setter() method called')
self.__age = value
xiaoli = Adult()
xiaoli.age = 19
print(xiaoli.age)輸出:
setter() method called
getter() method called
19
上面的代碼清晰地展示了如何用pythonic的方式使用@property裝飾器實(shí)現(xiàn)setter和getter屬性。同時(shí)實(shí)現(xiàn)了對(duì)屬性賦值時(shí)的有效性檢查。
到此這篇關(guān)于Python的getter和setter的方法使用詳解的文章就介紹到這了,更多相關(guān)Python的getter和setter的方法使用內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python安裝Scrapy庫(kù)的常見(jiàn)報(bào)錯(cuò)解決
本文主要介紹了Python安裝Scrapy庫(kù)的常見(jiàn)報(bào)錯(cuò)解決,文中通過(guò)圖文示例介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11
Python基于域相關(guān)實(shí)現(xiàn)圖像增強(qiáng)的方法教程
當(dāng)在圖像上訓(xùn)練深度神經(jīng)網(wǎng)絡(luò)模型時(shí),通過(guò)對(duì)由數(shù)據(jù)增強(qiáng)生成的更多圖像進(jìn)行訓(xùn)練,可以使模型更好地泛化。本文將為大家介紹Python基于域相關(guān)的圖像增強(qiáng)實(shí)現(xiàn)方法,需要的可以了解一下2022-01-01
python進(jìn)階教程之動(dòng)態(tài)類型詳解
這篇文章主要介紹了python進(jìn)階教程之動(dòng)態(tài)類型詳解,動(dòng)態(tài)類型是動(dòng)態(tài)語(yǔ)言的特性,本文對(duì)多種動(dòng)態(tài)類型應(yīng)用做了講解,需要的朋友可以參考下2014-08-08

