[Python基礎]裝飾器property 將一個方法轉換為屬性

2024/01/27閱讀時間約 9 分鐘

前幾篇討論到各種裝飾器的用法,本文將介紹另外一種裝飾器,可以將方法轉換成屬性來使用。

property也可以動態的取出物件的值,隨著時間其他運算改變所產生的值,讓我們繼續往下看更多介紹吧。

其他裝飾器的文章如下:

[Python基礎]裝飾器AbstractMethods 定義抽象方法

[Python基礎]裝飾器staticmethod 定義靜態方法

[Python基礎]裝飾器classmethod定義類別方法

[Python基礎]淺談裝飾器 decorator

使用 @property 裝飾器建立屬性


當你在 Python 中使用裝飾器 property 時,它通常用於設計 getter 和 setter 方法,以便更靈活地控制對類屬性的訪問和修改。

Property 的裝飾器主要有 3 種語法:

  • Getter: @property 取得屬性的值
  • Setter: @變數名稱.setter 設定屬性的值
  • Deleter: @變數名稱.deleter刪除屬性

程式範例

方法轉換為屬性

@property 裝飾器用在 radiusarea 方法上,分別定義了半徑和面積的屬性。這使得它們可以被像屬性一樣訪問,而不需要使用括號。

class Circle:
def __init__(self, radius):
self._radius = radius

@property
def radius(self):
return self._radius

@property
def area(self):
return 3.14 * self.radius**2

# 創建 Circle 實例
circle_instance = Circle(radius=5)

# 訪問 radius 屬性,實際上調用了 getter 方法
print(circle_instance.radius) # 輸出: 5

# 訪問 area 屬性,同樣調用了 getter 方法
print(circle_instance.area) # 輸出: 78.5

動態計算年齡

age屬性每次被訪問時都會計算當前年份出生年份之間差異返回動態計算的年齡值。這使得 age屬性的值能夠隨著時間的推移而變化,而不需要將年齡固定存儲。

from datetime import date

class Person:
def __init__(self, birth_year):
self._birth_year = birth_year

@property
def birth_year(self):
return self._birth_year

@property
def age(self):
current_year = date.today().year
return current_year - self.birth_year

# 創建 Person 實例
person_instance = Person(birth_year=1988)

# 訪問 age 屬性,動態計算並返回年齡
print(f'我今年是{person_instance.age}歲') # 依據當前年份和出生年份計算年齡

# 輸出
# 我今年是36歲​

property 裝飾器在 Python 中用於定義類的屬性,提供更靈活的方式來訪問、設定刪除屬性的值。

以下是 property 裝飾器的基本介紹,包括 gettersetter deleter 方法:

1. Getter 方法

Getter 方法負責取得屬性的值,使用 @property 裝飾器來定義。它使得你可以像訪問屬性一樣訪問這個方法,而不需要使用括號。

class MyClass:
def __init__(self, value):
self._value = value

@property
def my_property(self):
return self._value

# 使用 getter 取得屬性的值
my_instance = MyClass(value=42)
print(my_instance.my_property) # 輸出: 42

2. Setter 方法

Setter 方法負責設定屬性的值,使用 @<property_name>.setter 裝飾器來定義。它使得你可以像設定屬性一樣設定這個方法。

class MyClass:
def __init__(self, value):
self._value = value

@property
def my_property(self):
return self._value

@my_property.setter
def my_property(self, new_value):
self._value = new_value

# 使用 setter 設定屬性的值
my_instance = MyClass(value=42)
print(my_instance.my_property) # 輸出: 99
my_instance.my_property = 99 # new_value的值 覆蓋 self._value
print(my_instance.my_property) # 輸出: 99

property 裝飾器在 Python 中用於定義類的屬性,提供更靈活的方式來訪問、設定和刪除屬性的值。以下是 property 裝飾器的基本介紹,包括 getter、setter 和 deleter 方法:

1. Getter 方法

Getter 方法負責取得屬性的值,使用 @property 裝飾器來定義。它使得你可以像訪問屬性一樣訪問這個方法,而不需要使用括號。

pythonCopy codeclass MyClass:
def __init__(self, value):
self._value = value

@property
def my_property(self):
return self._value

# 使用 getter 取得屬性的值
my_instance = MyClass(value=42)
print(my_instance.my_property) # 輸出: 42

2. Setter 方法

Setter 方法負責設定屬性的值,使用 @<property_name>.setter 裝飾器來定義。它使得你可以像設定屬性一樣設定這個方法。

pythonCopy codeclass MyClass:
def __init__(self, value):
self._value = value

@property
def my_property(self):
return self._value

@my_property.setter
def my_property(self, new_value):
self._value = new_value

# 使用 setter 設定屬性的值
my_instance = MyClass(value=42)
my_instance.my_property = 99
print(my_instance.my_property) # 輸出: 99

3. Deleter 方法

Deleter 方法負責刪除屬性,使用 @<property_name>.deleter 裝飾器來定義。它使得你可以像刪除屬性一樣刪除這個方法。

class MyClass:
def __init__(self, value):
self._value = value

@property
def my_property(self):
return self._value

@my_property.deleter
def my_property(self):
del self._value

# 使用 deleter 刪除屬性
my_instance = MyClass(value=42)
del my_instance.my_property
# 此後再訪問 my_property 將引發 AttributeError,因為屬性已被刪除

試著呼叫看看,是不是真的刪除了。

刪除屬性在呼叫會引發錯誤

刪除屬性在呼叫會引發錯誤


這三種方法的使用讓你能夠更靈活地控制和管理類的屬性




49會員
88內容數
Python程式設計師,不是在學習就是在學習的路上
留言0
查看全部
發表第一個留言支持創作者!