網(wǎng)站首頁 編程語言 正文
前言
Python 動態(tài)屬性的概念可能會被面試問到,在項目當中也非常實用,但是在一般的編程教程中不會提到,可以進修一下。
先看一個簡單的例子。創(chuàng)建一個 Student 類,我希望通過實例來獲取每個學生的一些情況,包括名字,成績等。成績只有等到考試結束以后才會有,所以實例化的時候不會給它賦值。
class Student: def __init__(self, name): self.name = name self.score = None mike = Student('mike')
考試完以后,準備給 mike 打分:
mike.score = 999
在這里,老師一不小心多打了個 9 ,通常來說打分都是 100 分值,999 是一個非法數(shù)據(jù),不應該賦值成功。學生一多,老師打分出現(xiàn)手誤的情況肯定會越來越多,所以我們必須想辦法修改程序,限制 score 的值必須在 0-100 分。
限制值
我們定義一個方法,如果輸入的不是 0-100 的整數(shù),就讓程序報錯,數(shù)據(jù)合法,我們就把 score 屬性修改成功。
def set_score(self, new_score): if not isinstance(new_score, int): raise ValueError('score must be int') if 0 <= new_score <= 100: self.score = new_score return self.score else: raise ValueError('score invalid')
這樣我們每次需要獲取成績的時候使用 self.score 獲取,修改成績的時候調(diào)用函數(shù)來修改:
mike.set_score(999)
調(diào)用以后會報錯,因為 999 是非法數(shù)據(jù)。注意,這個時候我使用 self.score 還是可以進行設置,而且不報錯:
self.score = 999
這顯然是不行的。所以我們要提供一種機制,把 score 變成私有屬性,不能讓外部訪問。很遺憾,python 的私有屬性是偽私有。通常我們把 _
開頭的屬性叫私有屬性,但是這只是一種協(xié)議和規(guī)定,你看到下劃線開頭的屬性,不要去訪問了。你硬要訪問,是可以的,python 并不會禁止。
使用 @property 的方式代替。
上面的方法雖然實現(xiàn)了功能,但是改變了屬性的使用方式。平常是這樣使用的:
# 獲取屬性 a = mike.score # 設置屬性 mike.score = 99 @property def score(self): return self._score @score.setter def score(self, new_score): if not isinstance(new_score, int): raise ValueError('score must be int') if 0 <= new_score <= 100: self._score = new_score return self._score else: raise ValueError('score invalid')
動態(tài)屬性的好處
- 統(tǒng)一了調(diào)用方式。self.score = 99 的方式,而不是函數(shù)調(diào)用的方式。
-
_score
我們就不直接去使用了。你要用也可以,不建議。 - 如果我們一個屬性只可以讀,把 setter 部分注釋掉就可以了。
現(xiàn)在我們來完善這個類,添加 birth 屬性和年齡屬性:
from datetime import datetime class Student: def __init__(self, name, birth=1920): self.name = name self._score = None self.birth = birth self.age = datetime.now().year - self.birth mike = Student('mike') print(mike.birth) print(mike.age)
birth 和 age 這兩個是可以根據(jù)一個求出另外一個的。存在數(shù)據(jù)冗余問題。
-
age 屬性這樣是有問題的。mike 初始化的時候,age 已經(jīng)被求出來了,如果我在下一年再去訪問 age 屬性,那他就是個錯誤的值。可以通過把 age 設成現(xiàn)在的秒數(shù)來驗證:
self.age = datetime.now().second mike = Student('mike') time.sleep(5) print(mike.age) print(datetime.now().second)
動態(tài)顯示
@property def age(self): return datetime.now().year - self.birth
注意,這里不要去設置 @age.setter ,因為他是動態(tài)變化的,你修改了會造成數(shù)據(jù)不一致,它只能作為一個只讀屬性。
@property 作用和應用場景:
- @property 優(yōu)化了屬性讀取和設置的可讀性
- 需要限制屬性的特征;
- 只讀屬性。如果屬性只可以讀,不可以寫,用起來很方便。
- 這個屬性根據(jù)一個變化的環(huán)境動態(tài)改變。
附:用property代替getter和setter方法
>>>class Watermelon(): ? ? ? ?def __init__(self,price): ? ? ? ? ? ?self._price = price ? ? ? ? ? ? ? ? ?#私有屬性,外部無法修改和訪問 ? ? ? ?def get_price(self): ? ? ? ? ? ?return self._price ? ? ? ?def set_price(self,new_price): ? ? ? ? ? ?if new_price > 0: ? ? ? ? ? ? ? ?self._price = new_price ? ? ? ? ? ?else: ? ? ? ? ? ? ? ?raise 'error:價格必須大于零'
用property代替getter和setter
>>>class Watermelon(): def __init__(self,price): self._price = price @property #使用@property裝飾price方法 def price(self): return self._price @price.setter #使用@property裝飾方法,當對price賦值時,調(diào)用裝飾方法 def price(self,new_price): if new_price > 0: self._price = new_price else: raise 'error:價格必須大于零' >>> watermelon = Watermelon(4) >>> >>> watermelon.price 4 >>> >>> watermelon.price = 7 >>> >>> watermelon.price 7
總結
原文鏈接:https://juejin.cn/post/7084604734471929863
相關推薦
- 2022-09-24 ASP.NET?MVC實現(xiàn)多選下拉框_實用技巧
- 2023-06-19 深入了解Golang中Slice切片的使用_Golang
- 2022-04-01 SQL?Server?索引和視圖詳解_MsSql
- 2022-10-31 Kotlin中Object關鍵字的使用示例介紹_Android
- 2023-03-02 Kotlin關于協(xié)程是什么的探究_Android
- 2023-04-01 PyTorch之torch.randn()如何創(chuàng)建正態(tài)分布隨機數(shù)_python
- 2021-12-13 C語言輸出唯一的子串_C 語言
- 2022-10-10 ASP.NET?MVC使用jQuery?ui的progressbar實現(xiàn)進度條_實用技巧
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支