python @property的用法及含義全面解析


在接觸python時最開始接觸的代碼,取長方形的長和寬,定義一個長方形類,然后設置長方形的長寬屬性,通過實例化的方式調用長和寬,像如下代碼一樣。

class Rectangle(object):
  def __init__(self):
    self.width =10
    self.height=20
r=Rectangle()
print(r.width,r.height)

此時輸出結果為10 20

但是這樣在實際使用中會產生一個嚴重的問題,__init__ 中定義的屬性是可變的,換句話說,是使用一個系統的所有開發人員在知道屬性名的情況下,可以進行隨意的更改(盡管可能是在無意識的情況下),但這很容易造成嚴重的后果。

class Rectangle(object):
  def __init__(self):
    self.width =10
    self.height=20
r=Rectangle()
print(r.width,r.height)
r.width=1.0
print(r.width,r.height) 

以上代碼結果會輸出寬1.0,可能是開發人員不小心點了一個小數點上去,但是會系統的數據錯誤,並且在一些情況下很難排查。

這是生產中很不情願遇到的情況,這時候就考慮能不能將width屬性設置為私有的,其他人不能隨意更改的屬性,如果想要更改只能依照我的方法來修改,@property就起到這種作用(類似於java中的private)  

class Rectangle(object):
  @property
  def width(self):
    #變量名不與方法名重復,改為true_width,下同
    return self.true_width
 
  @property
  def height(self):
    return self.true_height
s = Rectangle()
#與方法名一致
s.width = 1024
s.height = 768
print(s.width,s.height)

(@property使方法像屬性一樣調用,就像是一種特殊的屬性)

此時,如果在外部想要給width重新直接賦值就會報AttributeError: can't set attribute的錯誤,這樣就保證的屬性的安全性。

同樣為了解決對屬性的操作,提供了封裝方法的方式進行屬性的修改

class Rectangle(object):
  @property
  def width(self):
    # 變量名不與方法名重復,改為true_width,下同
    return self.true_width
  @width.setter
  def width(self, input_width):
    self.true_width = input_width
  @property
  def height(self):
    return self.true_height
  @height.setter
  #與property定義的方法名要一致
  def height(self, input_height):
    self.true_height = input_height
s = Rectangle()
# 與方法名一致
s.width = 1024
s.height = 768
print(s.width,s.height)

此時就可以對“屬性”進行賦值操作,同樣的方法還del,用處是刪除屬性,寫法如下,具體實現不在贅述

@height.deleter
def height(self):
    del self.true_height

總結一下@property提供了可讀可寫可刪除的操作,如果像只讀效果,就只需要定義@property就可以,不定義代表禁止其他操作。

本文轉自:https://www.jb51.net/article/134148.htm

在接觸python時最開始接觸的代碼,取長方形的長和寬,定義一個長方形類,然后設置長方形的長寬屬性,通過實例化的方式調用長和寬,像如下代碼一樣。

?
1
2
3
4
5
6
class Rectangle(object):
   def __init__(self):
     self.width =10
     self.height=20
r=Rectangle()
print(r.width,r.height)

此時輸出結果為10 20

但是這樣在實際使用中會產生一個嚴重的問題,__init__ 中定義的屬性是可變的,換句話說,是使用一個系統的所有開發人員在知道屬性名的情況下,可以進行隨意的更改(盡管可能是在無意識的情況下),但這很容易造成嚴重的后果。

?
1
2
3
4
5
6
7
8
class Rectangle(object):
   def __init__(self):
     self.width =10
     self.height=20
r=Rectangle()
print(r.width,r.height)
r.width=1.0
print(r.width,r.height)

以上代碼結果會輸出寬1.0,可能是開發人員不小心點了一個小數點上去,但是會系統的數據錯誤,並且在一些情況下很難排查。

這是生產中很不情願遇到的情況,這時候就考慮能不能將width屬性設置為私有的,其他人不能隨意更改的屬性,如果想要更改只能依照我的方法來修改,@property就起到這種作用(類似於java中的private)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Rectangle(object):
   @property
   def width(self):
     #變量名不與方法名重復,改為true_width,下同
     return self.true_width
 
   @property
   def height(self):
     return self.true_height
s = Rectangle()
#與方法名一致
s.width = 1024
s.height = 768
print(s.width,s.height)

(@property使方法像屬性一樣調用,就像是一種特殊的屬性)

此時,如果在外部想要給width重新直接賦值就會報AttributeError: can't set attribute的錯誤,這樣就保證的屬性的安全性。

同樣為了解決對屬性的操作,提供了封裝方法的方式進行屬性的修改

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Rectangle(object):
   @property
   def width(self):
     # 變量名不與方法名重復,改為true_width,下同
     return self.true_width
   @width.setter
   def width(self, input_width):
     self.true_width = input_width
   @property
   def height(self):
     return self.true_height
   @height.setter
   #與property定義的方法名要一致
   def height(self, input_height):
     self.true_height = input_height
s = Rectangle()
# 與方法名一致
s.width = 1024
s.height = 768
print(s.width,s.height)

此時就可以對“屬性”進行賦值操作,同樣的方法還del,用處是刪除屬性,寫法如下,具體實現不在贅述。

?
1
2
3
@height.deleter
def height(self):
     del self.true_height

總結一下@property提供了可讀可寫可刪除的操作,如果像只讀效果,就只需要定義@property就可以,不定義代表禁止其他操作。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM