Python中property屬性的用處詳解
前言
Python 動態(tài)屬性的概念可能會被面試問到,在項目當中也非常實用,但是在一般的編程教程中不會提到,可以進修一下。
先看一個簡單的例子。創(chuàng)建一個 Student 類,我希望通過實例來獲取每個學生的一些情況,包括名字,成績等。成績只有等到考試結(jié)束以后才會有,所以實例化的時候不會給它賦值。
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
總結(jié)
到此這篇關(guān)于Python中property屬性用處的文章就介紹到這了,更多相關(guān)Python中property屬性內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 掌握Python property裝飾器巧妙管理類的屬性
- python裝飾器中@property屬性的使用解析
- Python中通過property設置類屬性的訪問
- 關(guān)于python中@property的使用方法
- Python?property裝飾器使用案例介紹
- Python深入分析@property裝飾器的應用
- python 中的@property的用法詳解
- python中@Property屬性使用方法
- Python裝飾器中@property使用詳解
- Python中關(guān)于property使用的小技巧
- Python的@property的使用
- 詳解Python裝飾器之@property
- Python property函數(shù)的具體使用
相關(guān)文章
python使用win32com在百度空間插入html元素示例
這篇文章主要介紹了python使用win32com在百度空間插入html元素的示例,大家參考使用吧2014-02-02
使用Python和XML實現(xiàn)文件復制工具的完整代碼
在本篇博客中,我們將學習如何使用 wxPython 構(gòu)建一個簡單的文件復制工具,并將文件路徑和目標目錄的配置信息保存到 XML 文件中,通過這種方式,我們可以在下次運行程序時輕松加載之前保存的配置,需要的朋友可以參考下2024-08-08
使用opencv相關(guān)函數(shù)確定圖片中的直線問題
這篇文章主要介紹了使用opencv相關(guān)函數(shù)確定圖片中的直線問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11

