日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
淺析Python裝飾器中的@property

一、使用@property優(yōu)點(diǎn)

將類方法轉(zhuǎn)換為類屬性,可以用來直接獲取屬性值或者對(duì)屬性進(jìn)行賦值。

紅寺堡網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)!從網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站開發(fā)等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營維護(hù)。創(chuàng)新互聯(lián)于2013年成立到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)

案例分析

例:

 
 
 
 
  1. class Exam(object): 
  2.     def __init__(self, score): 
  3.         self._score = score 
  4.  
  5.     def get_score(self): 
  6.         return self._score 
  7.  
  8.     def set_score(self, val): 
  9.         if val < 0: 
  10.             self._score = 0 
  11.         elif val > 100: 
  12.             self._score = 100 
  13.         else: 
  14.             self._score = val 
  15.  
  16. e = Exam(60) 
  17. print(e.get_score()) 
  18.  
  19. e.set_score(70) 
  20. print(e.get_score()) 

代碼解析:

定義了一個(gè) Exam 類,為了避免直接對(duì) _score 屬性操作,提供了 get_score 和 set_score 方法,這樣起到了封裝的作用,把一些不想對(duì)外公開的屬性隱蔽起來,而只是提供方法給用戶操作,在方法里面,可以檢查參數(shù)的合理性等。

Python 提供了 property 裝飾器,被裝飾的方法,可以將其『當(dāng)作』屬性來用。

例 :

 
 
 
 
  1. class Exam(object): 
  2.     def __init__(self, score): 
  3.         self._score = score 
  4.  
  5.     @property 
  6.     def score(self): 
  7.         return self._score 
  8.  
  9.     @score.setter 
  10.     def score(self, val): 
  11.         if val < 0: 
  12.             self._score = 0 
  13.         elif val > 100: 
  14.             self._score = 100 
  15.         else: 
  16.             self._score = val 
  17.  
  18.  
  19. e = Exam(60) 
  20. print(e.score) 
  21.  
  22. e.score = 90 
  23. print(e.score) 
  24.  
  25. e.score = 200 
  26. print(e.score) 

注:

給方法 score 加上了 @property,于是可以把 score 當(dāng)成一個(gè)屬性來用,此時(shí),又會(huì)創(chuàng)建新的score.setter,它可以把被裝飾的方法變成屬性來賦值。

另外,也不一定要使用 score.setter 這個(gè)裝飾器,這時(shí) score 就變成一個(gè)只讀屬性:

 
 
 
 
  1. class Exam(object): 
  2.     def __init__(self, score): 
  3.         self._score = score 
  4.  
  5.     @property 
  6.     def score(self): 
  7.         return self._score 
  8.  
  9. e = Exam(60) 
  10. print(e.score) 
  11. e.score = 200  # score 是只讀屬性,不能設(shè)置值 
  12. print(e.score) 

二、@property的力量

python處理上述問題的方法是使用property。可以這樣來實(shí)現(xiàn)它。

例 :

 
 
 
 
  1. class Celsius: 
  2.     def __init__(self, temperature = 0): 
  3.         self.temperature = temperature 
  4.  
  5.     def to_fahrenheit(self): 
  6.         return (self.temperature * 1.8) + 32 
  7.  
  8.     def get_temperature(self): 
  9.         print("獲得的值") 
  10.         return self._temperature 
  11.  
  12.     def set_temperature(self, value): 
  13.         if value < -273: 
  14.             raise ValueError("零下273度是不可能的") 
  15.         print("設(shè)定值") 
  16.         self._temperature = value 
  17.  
  18.     temperature = property(get_temperature,set_temperature) 

并且,一旦運(yùn)行,在shell中發(fā)出以下代碼。

 
 
 
 
  1. c = Celsius() 
  2. print(c.temperature) 

創(chuàng)建對(duì)象時(shí),將調(diào)用init ()方法。此方法的線為self.temperature = temperature。

此分配自動(dòng)稱為set_temperature()。

2. 屬性的作用。

任何訪問如c.temperature都會(huì)自動(dòng)調(diào)用get_temperature()。

例:

 
 
 
 
  1. c.temperature = 37 
  2. print(c.temperature) 
  3. print(c.to_fahrenheit()) 

注:

溫度值存儲(chǔ)在私有變量_temperature中。temperature屬性是一個(gè)屬性對(duì)象,它提供了與此私有變量的接口。

三、深入了解property

在Python中,property()是一個(gè)內(nèi)置函數(shù),用于創(chuàng)建并返回屬性對(duì)象。

語法

 
 
 
 
  1. property(fget=None, fset=None, fdel=None, doc=None) 

參數(shù)解析

fget為獲取屬性值的函數(shù),fset為設(shè)置屬性值的函數(shù),fdel為刪除屬性的函數(shù),doc為字符串(如注釋)。從實(shí)現(xiàn)中可以看出,這些函數(shù)參數(shù)是可選的。

可以簡(jiǎn)單地按照以下方式創(chuàng)建屬性對(duì)象。

 
 
 
 
  1. property(fget=None, fset=None, fdel=None, doc=None) 
  2. print(property()) 

1. 屬性對(duì)象有三個(gè)方法,getter()、setter()和deleter()。

語法:

 
 
 
 
  1. temperature = property(get_temperature,set_temperature) 

用于稍后指定fget、fset和fdel。

 
 
 
 
  1. # 創(chuàng)建空屬性 
  2. temperature = property() 
  3. # 設(shè)置 fget 
  4. temperature = temperature.getter(get_temperature) 
  5. # 設(shè)置 fset 
  6. temperature = temperature.setter(set_temperature) 

注:

這兩段代碼是等效的。

不定義名稱get_temperature,set_temperature。

因?yàn)樗鼈兪遣槐匾模⑶視?huì)影響類命名空間。為此,在定義getter和setter函數(shù)時(shí)重用了名稱temperature。

2. 案例

例:

 
 
 
 
  1. class Celsius: 
  2.     def __init__(self, temperature = 0): 
  3.         self._temperature = temperature 
  4.  
  5.     def to_fahrenheit(self): 
  6.         return (self.temperature * 1.8) + 32 
  7.  
  8.     @property 
  9.     def temperature(self): 
  10.         print("獲得值") 
  11.         return self._temperature 
  12.  
  13.     @temperature.setter 
  14.     def temperature(self, value): 
  15.         if value < -273: 
  16.             raise ValueError("零下273度是不可能的") 
  17.         print("零下273度是不可能的") 
  18.         self._temperature = value 
  19. c=Celsius() 
  20. c.temperature = 37 
  21. print(c.temperature) 

注:

實(shí)現(xiàn)是制作屬性的簡(jiǎn)單方法和推薦方法。在Python中尋找屬性時(shí),很可能會(huì)遇到這些類型的構(gòu)造。

四、總結(jié)

本文基于Python基礎(chǔ),介紹了@property 如何把方法變成了屬性。通過案例的分析,代碼的展示。介紹了@property的力量,以及提供了相應(yīng)錯(cuò)誤的解決方案處理方法。屬性的作用。

歡迎大家積極嘗試,有時(shí)候看到別人實(shí)現(xiàn)起來很簡(jiǎn)單,但是到自己動(dòng)手實(shí)現(xiàn)的時(shí)候,總會(huì)有各種各樣的問題,切勿眼高手低,勤動(dòng)手,才可以理解的更加深刻。

代碼很簡(jiǎn)單,希望對(duì)你學(xué)習(xí)有幫助。


網(wǎng)頁題目:淺析Python裝飾器中的@property
分享網(wǎng)址:http://www.5511xx.com/article/dpidgog.html