Python中的getter和setter的方法使用詳解
本文主要內(nèi)容:
- 解釋setter和getter的使用方法解釋@property裝飾器的妙用
- 在python中,setter和getter方法并不像其它編程語言中的那樣?;旧希诿嫦?qū)ο缶幊陶Z言中,使用setter和getter方法的主要目的是為了確保數(shù)據(jù)的封裝。不像其它面向?qū)ο缶幊陶Z言,python中的私有變量并不是真正的隱藏字段。在python中,通常在以下情況會用到setter和getter方法:
- 在獲取或者設(shè)置屬性值的時候使用setter和getter方法為其添加驗證邏輯避免對類的某些字段直接訪問,比如類的私有變量不應(yīng)該被外部調(diào)用者直擊訪問或者修改
使用普通函數(shù)實現(xiàn)setter和getter方法
要實現(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()方法與普通方法并沒有什么兩樣,那么如何實現(xiàn)像getter和setter一樣的功能呢?這就要用到python中的特殊方法property()。
使用property()方法來實現(xiàn)setter和getter的行為
property()是python中的一個內(nèi)置方法,它創(chuàng)建并返回一個屬性對象。一個屬性對象有三個方法,getter()、setter()和delete()。property()內(nèi)置方法有四個參數(shù),property(fget,fset, fdel, doc)。fget是一個用于獲取屬性值的函數(shù),fset是一個用于設(shè)置屬性值的函數(shù),fdel是一個用于刪除屬性的函數(shù),doc用于為屬性創(chuàng)建文檔說明。一個屬性兌現(xiàn)有三個方法,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就是一個屬性對象,它保證了對私有變量的安全訪問。
使用@property裝飾器來實現(xiàn)setter和getter的行為
除了上面使用property()的方法來實現(xiàn)getter、setter方法的行為,在python中還可以裝飾器@property來實現(xiàn)。@property是python的一個內(nèi)置裝飾器,使用裝飾器的目的是改變類的方法或者屬性,這樣調(diào)用者就無需在代碼中做任何改動。
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裝飾器實現(xiàn)setter和getter屬性。同時實現(xiàn)了對屬性賦值時的有效性檢查。
到此這篇關(guān)于Python的getter和setter的方法使用詳解的文章就介紹到這了,更多相關(guān)Python的getter和setter的方法使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python3 圖片referer防盜鏈的實現(xiàn)方法
本篇文章主要介紹了python3 圖片referer防盜鏈的實現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03python題解LeetCode303區(qū)域和檢索示例詳解
這篇文章主要為大家介紹了python題解LeetCode303區(qū)域和檢索示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12對django 2.x版本中models.ForeignKey()外鍵說明介紹
這篇文章主要介紹了對django 2.x版本中models.ForeignKey()外鍵說明介紹,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03python自動化測試selenium核心技術(shù)等待條件教程
這篇文章主要為大家介紹了python自動化測試selenium核心技術(shù)等待條件教程的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-11-11django 連接數(shù)據(jù)庫出現(xiàn)1045錯誤的解決方式
這篇文章主要介紹了django 連接數(shù)據(jù)庫出現(xiàn)1045錯誤的解決方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05