成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

淺析Python裝飾器中的@property

開發(fā) 后端
本文基于Python基礎,介紹了@property 如何把方法變成了屬性。通過案例的分析,代碼的展示。介紹了@property的力量,以及提供了相應錯誤的解決方案處理方法。屬性的作用。

[[392255]]

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

將類方法轉換為類屬性,可以用來直接獲取屬性值或者對屬性進行賦值。

案例分析

例:

  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()) 

代碼解析:

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

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

例 :

  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 當成一個屬性來用,此時,又會創(chuàng)建新的score.setter,它可以把被裝飾的方法變成屬性來賦值。

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

  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 是只讀屬性,不能設置值 
  12. print(e.score) 

二、@property的力量

python處理上述問題的方法是使用property。可以這樣來實現(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("設定值"
  16.         self._temperature = value 
  17.  
  18.     temperature = property(get_temperature,set_temperature) 

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

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

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

此分配自動稱為set_temperature()。

2. 屬性的作用。

任何訪問如c.temperature都會自動調用get_temperature()。

例:

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

注:

溫度值存儲在私有變量_temperature中。temperature屬性是一個屬性對象,它提供了與此私有變量的接口。

三、深入了解property

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

語法

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

參數(shù)解析

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

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

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

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

語法:

  1. temperature = property(get_temperature,set_temperature) 

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

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

注:

這兩段代碼是等效的。

不定義名稱get_temperature,set_temperature。

因為它們是不必要的,并且會影響類命名空間。為此,在定義getter和setter函數(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) 

注:

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

四、總結

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

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

代碼很簡單,希望對你學習有幫助。

 

責任編輯:姜華 來源: Go語言進階學習
相關推薦

2021-02-01 14:17:53

裝飾器外層函數(shù)里層函數(shù)

2020-11-17 09:10:44

裝飾器

2025-01-22 15:58:46

2021-04-15 15:20:46

PythonProperty裝飾器

2011-07-19 17:18:35

Objective-C Property

2016-11-01 09:24:38

Python裝飾器

2010-02-01 17:50:32

Python裝飾器

2023-02-07 07:47:52

Python裝飾器函數(shù)

2024-05-24 11:36:28

Python裝飾器

2022-09-19 23:04:08

Python裝飾器語言

2024-09-12 15:32:35

裝飾器Python

2010-02-01 14:28:37

Python全局變量

2025-04-14 08:35:00

Python類裝飾器裝飾器

2021-07-27 15:58:12

Python日志代碼

2022-09-27 11:01:08

Python裝飾器

2021-06-01 07:19:58

Python函數(shù)裝飾器

2023-12-11 15:51:00

Python裝飾器代碼

2024-05-10 12:33:06

flask裝飾器

2025-07-01 09:46:30

2023-12-13 13:28:16

裝飾器模式Python設計模式
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 亚洲精品一区二区三区四区高清 | 国产在线观看不卡一区二区三区 | 日韩色在线| 国产综合精品一区二区三区 | 在线观看视频一区二区三区 | 亚洲狠狠丁香婷婷综合久久久 | www.精品一区| 午夜精品在线观看 | 91精品久久久久久综合五月天 | 国产目拍亚洲精品99久久精品 | 午夜精品久久久久久久久久久久 | 久久久久中文字幕 | 国产精品欧美精品日韩精品 | 国产精品一区二 | 免费久久视频 | 国产精品久久久久久久久久久久久 | 欧美日韩a | 国产精品欧美一区喷水 | 日韩视频在线免费观看 | 欧美日本高清 | 日本在线看片 | 黄色一级片视频 | 黑人性hd | 999免费观看视频 | 国产www成人 | 日韩在线观看网站 | 国产超碰人人爽人人做人人爱 | 久久久久久亚洲精品不卡 | 亚洲视频二区 | 狠狠色综合网站久久久久久久 | 午夜免费在线电影 | 伊人精品一区二区三区 | 欧美日韩不卡合集视频 | av毛片| 精品日韩一区二区 | 欧美精品一区二区三区在线播放 | 91在线精品视频 | 欧美在线一区二区三区 | 伊人在线视频 | 蜜桃免费av | 午夜影视|