網站首頁 編程語言 正文
本文主要內容:
- 解釋setter和getter的使用方法解釋@property裝飾器的妙用
- 在python中,setter和getter方法并不像其它編程語言中的那樣。基本上,在面向對象編程語言中,使用setter和getter方法的主要目的是為了確保數據的封裝。不像其它面向對象編程語言,python中的私有變量并不是真正的隱藏字段。在python中,通常在以下情況會用到setter和getter方法:
- 在獲取或者設置屬性值的時候使用setter和getter方法為其添加驗證邏輯避免對類的某些字段直接訪問,比如類的私有變量不應該被外部調用者直擊訪問或者修改
使用普通函數實現setter和getter方法
要實現setter和getter屬性,只是定義普通方法get()和set()并不能反產生任何特殊的行為,例如:
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方法設置age
xiaoming.set(20)
# 使用getter方法返回age
print(xiaoming.get())
print(xiaoming._age)
輸出:
20
20
在上面代碼中,set_age()和get_age()方法與普通方法并沒有什么兩樣,那么如何實現像getter和setter一樣的功能呢?這就要用到python中的特殊方法property()。
使用property()方法來實現setter和getter的行為
property()是python中的一個內置方法,它創建并返回一個屬性對象。一個屬性對象有三個方法,getter()、setter()和delete()。property()內置方法有四個參數,property(fget,fset, fdel, doc)。fget是一個用于獲取屬性值的函數,fset是一個用于設置屬性值的函數,fdel是一個用于刪除屬性的函數,doc用于為屬性創建文檔說明。一個屬性兌現有三個方法,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
# 設置屬性_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裝飾器來實現setter和getter的行為
除了上面使用property()的方法來實現getter、setter方法的行為,在python中還可以裝飾器@property來實現。@property是python的一個內置裝飾器,使用裝飾器的目的是改變類的方法或者屬性,這樣調用者就無需在代碼中做任何改動。
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裝飾器實現setter和getter屬性。同時實現了對屬性賦值時的有效性檢查。
原文鏈接:https://blog.csdn.net/weixin_73136678/article/details/128190690
相關推薦
- 2022-09-22 使用 sed 替換字符串中最后一次出現的字符
- 2022-09-18 Pandas?Query方法使用深度總結_python
- 2022-12-26 C語言逆向分析語法超詳細分析_C 語言
- 2022-07-21 pandas合并操作
- 2022-09-03 redis?主從哨兵模式實現一主二從_Redis
- 2024-04-23 Win11老是提示資源管理器已停止工作怎么解決
- 2022-09-07 C++實現哈希散列表的示例_C 語言
- 2022-12-25 利用pycharm調試ssh遠程程序并實時同步文件的操作方法_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支