Python進階:@property 動態屬性
Python 動態屬性的概念可能會被面試問到,在項目當中也非常實用,但是在一般的編程教程中不會提到,可以進修一下。
先看一個簡單的例子。創建一個 Student 類,我希望通過實例來獲取每個學生的一些情況,包括名字,成績等。成績只有等到考試結束以后才會有,所以實例化的時候不會給它賦值。
class Student: def __init__(self, name): self.name = name self.score = None mike = Student('mike')
考試完以后,准備給 mike 打分:
mike.score = 999
在這里,老師一不小心多打了個 9 ,通常來說打分都是 100 分值,999 是一個非法數據,不應該賦值成功。學生一多,老師打分出現手誤的情況肯定會越來越多,所以我們必須想辦法修改程序,限制 score 的值必須在 0-100 分。
限制值
我們定義一個方法,如果輸入的不是 0-100 的整數,就讓程序報錯,數據合法,我們就把 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 獲取,修改成績的時候調用函數來修改:
mike.set_score(999)
調用以后會報錯,因為 999 是非法數據。注意,這個時候我使用 self.score 還是可以進行設置,而且不報錯:
self.score = 999
這顯然是不行的。所以我們要提供一種機制,把 score 變成私有屬性,不能讓外部訪問。很遺憾,python 的私有屬性是偽私有。通常我們把 _ 開頭的屬性叫私有屬性,但是這只是一種協議和規定,你看到下划線開頭的屬性,不要去訪問了。你硬要訪問,是可以的,python 並不會禁止。
使用 @property 的方式代替。
上面的方法雖然實現了功能,但是改變了屬性的使用方式。平常是這樣使用的:
# 獲取屬性 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')
動態屬性的好處
- 統一了調用方式。self.score = 99 的方式,而不是函數調用的方式。
_score我們就不直接去使用了。你要用也可以,不建議。- 如果我們一個屬性只可以讀,把 setter 部分注釋掉就可以了。
現在我們來完善這個類,添加 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 這兩個是可以根據一個求出另外一個的。存在數據冗余問題。
-
age 屬性這樣是有問題的。mike 初始化的時候,age 已經被求出來了,如果我在下一年再去訪問 age 屬性,那他就是個錯誤的值。可以通過把 age 設成現在的秒數來驗證:
self.age = datetime.now().second mike = Student('mike') time.sleep(5) print(mike.age) print(datetime.now().second)
動態顯示
@property def age(self): return datetime.now().year - self.birth
注意,這里不要去設置 @age.setter ,因為他是動態變化的,你修改了會造成數據不一致,它只能作為一個只讀屬性。
@property 作用和應用場景:
- @property 優化了屬性讀取和設置的可讀性
- 需要限制屬性的特征;
- 只讀屬性。如果屬性只可以讀,不可以寫,用起來很方便。
- 這個屬性根據一個變化的環境動態改變。
