五月综合激情婷婷六月,日韩欧美国产一区不卡,他扒开我内裤强吻我下面视频 ,无套内射无矿码免费看黄,天天躁,日日躁,狠狠躁

新聞動(dòng)態(tài)

Python 使用@property對(duì)屬性進(jìn)行數(shù)據(jù)規(guī)范性校驗(yàn)的實(shí)現(xiàn)

發(fā)布日期:2021-12-22 21:38 | 文章來(lái)源:CSDN

在綁定屬性時(shí),如果我們直接把屬性暴露出去,雖然寫(xiě)起來(lái)很簡(jiǎn)單,但是,沒(méi)辦法檢查參數(shù),導(dǎo)致可以把成績(jī)隨便改:

s = Student()
s.score = 9999

這顯然不合邏輯。為了限制score的范圍,可以通過(guò)一個(gè)​​set_score()​​方法來(lái)設(shè)置成績(jī),再通過(guò)一個(gè)​​get_score()​​來(lái)獲取成績(jī),這樣,在​​set_score()​​方法里,就可以檢查參數(shù):

class Student(object):
 def get_score(self):
return self._score
 def set_score(self, value):
  if not isinstance(value, int):
raise ValueError('score must be an integer!')
  if value < 0 or value > 100:
raise ValueError('score must between 0 ~ 100!')
  self._score = value

現(xiàn)在,對(duì)任意的Student實(shí)例進(jìn)行操作,就不能隨心所欲地設(shè)置score了:

>>> s = Student()
>>> s.set_score(60) # ok!
>>> s.get_score()
60
>>> s.set_score(9999)
Traceback (most recent call last):
  ...
ValueError: score must between 0 ~ 100!

但是,上面的調(diào)用方法又略顯復(fù)雜,沒(méi)有直接用屬性這么直接簡(jiǎn)單。

有沒(méi)有既能檢查參數(shù),又可以用類(lèi)似屬性這樣簡(jiǎn)單的方式來(lái)訪(fǎng)問(wèn)類(lèi)的變量呢?對(duì)于追求完美的Python程序員來(lái)說(shuō),這是必須要做到的!

還記得裝飾器(decorator)可以給函數(shù)動(dòng)態(tài)加上功能嗎?對(duì)于類(lèi)的方法,裝飾器一樣起作用。Python內(nèi)置的​​@property​​裝飾器就是負(fù)責(zé)把一個(gè)方法變成屬性調(diào)用的:

class Student(object):
 @property
 def score(self):
  return self._score
 @score.setter
 def score(self, value):
  if not isinstance(value, int):
raise ValueError('score must be an integer!')
  if value < 0 or value > 100:
raise ValueError('score must between 0 ~ 100!')
  self._score = value

​​@property​​的實(shí)現(xiàn)比較復(fù)雜,我們先考察如何使用。把一個(gè)getter方法變成屬性,只需要加上​​@property​​就可以了,此時(shí),​​@property​​本身又創(chuàng)建了另一個(gè)裝飾器​​@score.setter​​,負(fù)責(zé)把一個(gè)setter方法變成屬性賦值,于是,我們就擁有一個(gè)可控的屬性操作:

>>> s = Student()
>>> s.score = 60 # OK,實(shí)際轉(zhuǎn)化為s.set_score(60)
>>> s.score # OK,實(shí)際轉(zhuǎn)化為s.get_score()
60
>>> s.score = 9999
Traceback (most recent call last):
  ...
ValueError: score must between 0 ~ 100!

注意到這個(gè)神奇的​​@property​​,我們?cè)趯?duì)實(shí)例屬性操作的時(shí)候,就知道該屬性很可能不是直接暴露的,而是通過(guò)getter和setter方法來(lái)實(shí)現(xiàn)的。

還可以定義只讀屬性,只定義getter方法,不定義setter方法就是一個(gè)只讀屬性:

class Student(object):
 @property
 def birth(self):
  return self._birth
 @birth.setter
 def birth(self, value):
  self._birth = value
 @property
 def age(self):
  return 2015 - self._birth

上面的​​birth​​是可讀寫(xiě)屬性,而​​age​​就是一個(gè)只讀屬性,因?yàn)?#8203;​age​​可以根據(jù)​​birth​​和當(dāng)前時(shí)間計(jì)算出來(lái)。

要特別注意:屬性的方法名不要和實(shí)例變量重名。例如,以下的代碼是錯(cuò)誤的:

class Student(object):
 # 方法名稱(chēng)和實(shí)例變量均為birth:
 @property
 def birth(self):
  return self.birth

這是因?yàn)檎{(diào)用​​s.birth​​時(shí),首先轉(zhuǎn)換為方法調(diào)用,在執(zhí)行​​return self.birth​​時(shí),又視為訪(fǎng)問(wèn)​​self​​的屬性,于是又轉(zhuǎn)換為方法調(diào)用,造成無(wú)限遞歸,最終導(dǎo)致棧溢出報(bào)錯(cuò)​​RecursionError​​。

小結(jié)

​​@property​​廣泛應(yīng)用在類(lèi)的定義中,可以讓調(diào)用者寫(xiě)出簡(jiǎn)短的代碼,同時(shí)保證對(duì)參數(shù)進(jìn)行必要的檢查,這樣,程序運(yùn)行時(shí)就減少了出錯(cuò)的可能性。

練習(xí)

請(qǐng)利用​​@property​​給一個(gè)​​Screen​​對(duì)象加上​​width​​和​​height​​屬性,以及一個(gè)只讀屬性​​resolution​​:

# -*- coding: utf-8 -*-
# 測(cè)試:
s = Screen()
s.width = 1024
s.height = 768
print('resolution =', s.resolution)
if s.resolution == 786432:
print('測(cè)試通過(guò)!')
else:
print('測(cè)試失敗!')

完整源碼:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
class Student(object):
@property
def score(self):
return self._score
@score.setter
def score(self, value):
if not isinstance(value, int):
raise ValueError('score must be an integer!')
if value < 0 or value > 100:
raise ValueError('score must between 0 ~ 100!')
self._score = value
s = Student()
s.score = 60
print('s.score =', s.score)
# ValueError: score must between 0 ~ 100!
s.score = 9999

到此這篇關(guān)于Python 使用@property對(duì)屬性進(jìn)行數(shù)據(jù)規(guī)范性校驗(yàn)的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Python @property 屬性校驗(yàn)內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!

版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實(shí)時(shí)開(kāi)通

自選配置、實(shí)時(shí)開(kāi)通

免備案

全球線(xiàn)路精選!

全天候客戶(hù)服務(wù)

7x24全年不間斷在線(xiàn)

專(zhuān)屬顧問(wèn)服務(wù)

1對(duì)1客戶(hù)咨詢(xún)顧問(wèn)

在線(xiàn)
客服

在線(xiàn)客服:7*24小時(shí)在線(xiàn)

客服
熱線(xiàn)

400-630-3752
7*24小時(shí)客服服務(wù)熱線(xiàn)

關(guān)注
微信

關(guān)注官方微信
頂部