
- Python 基礎
- Python - 首頁
- Python - 概述
- Python - 歷史
- Python - 特性
- Python vs C++
- Python - Hello World 程式
- Python - 應用領域
- Python - 直譯器
- Python - 環境搭建
- Python - 虛擬環境
- Python - 基本語法
- Python - 變數
- Python - 資料型別
- Python - 型別轉換
- Python - Unicode 系統
- Python - 字面量
- Python - 運算子
- Python - 算術運算子
- Python - 比較運算子
- Python - 賦值運算子
- Python - 邏輯運算子
- Python - 位運算子
- Python - 成員運算子
- Python - 身份運算子
- Python - 運算子優先順序
- Python - 註釋
- Python - 使用者輸入
- Python - 數字
- Python - 布林值
- Python 控制語句
- Python - 控制流
- Python - 決策制定
- Python - If 語句
- Python - If else
- Python - 巢狀 If
- Python - Match-Case 語句
- Python - 迴圈
- Python - for 迴圈
- Python - for-else 迴圈
- Python - While 迴圈
- Python - break 語句
- Python - continue 語句
- Python - pass 語句
- Python - 巢狀迴圈
- Python 函式 & 模組
- Python - 函式
- Python - 預設引數
- Python - 關鍵字引數
- Python - 僅限關鍵字引數
- Python - 位置引數
- Python - 僅限位置引數
- Python - 可變引數
- Python - 變數作用域
- Python - 函式註解
- Python - 模組
- Python - 內建函式
- Python 字串
- Python - 字串
- Python - 字串切片
- Python - 修改字串
- Python - 字串連線
- Python - 字串格式化
- Python - 跳脫字元
- Python - 字串方法
- Python - 字串練習
- Python 列表
- Python - 列表
- Python - 訪問列表項
- Python - 修改列表項
- Python - 新增列表項
- Python - 刪除列表項
- Python - 迴圈列表
- Python - 列表推導式
- Python - 排序列表
- Python - 複製列表
- Python - 合併列表
- Python - 列表方法
- Python - 列表練習
- Python 元組
- Python - 元組
- Python - 訪問元組項
- Python - 更新元組
- Python - 解包元組
- Python - 迴圈元組
- Python - 合併元組
- Python - 元組方法
- Python - 元組練習
- Python 集合
- Python - 集合
- Python - 訪問集合項
- Python - 新增集合項
- Python - 刪除集合項
- Python - 迴圈集合
- Python - 合併集合
- Python - 複製集合
- Python - 集合運算子
- Python - 集合方法
- Python - 集合練習
- Python 字典
- Python - 字典
- Python - 訪問字典項
- Python - 修改字典項
- Python - 新增字典項
- Python - 刪除字典項
- Python - 字典檢視物件
- Python - 迴圈字典
- Python - 複製字典
- Python - 巢狀字典
- Python - 字典方法
- Python - 字典練習
- Python 陣列
- Python - 陣列
- Python - 訪問陣列項
- Python - 新增陣列項
- Python - 刪除陣列項
- Python - 迴圈陣列
- Python - 複製陣列
- Python - 反轉陣列
- Python - 排序陣列
- Python - 合併陣列
- Python - 陣列方法
- Python - 陣列練習
- Python 檔案處理
- Python - 檔案處理
- Python - 寫入檔案
- Python - 讀取檔案
- Python - 重新命名和刪除檔案
- Python - 目錄
- Python - 檔案方法
- Python - OS 檔案/目錄方法
- Python - OS 路徑方法
- 面向物件程式設計
- Python - OOPs 概念
- Python - 類 & 物件
- Python - 類屬性
- Python - 類方法
- Python - 靜態方法
- Python - 建構函式
- Python - 訪問修飾符
- Python - 繼承
- Python - 多型
- Python - 方法重寫
- Python - 方法過載
- Python - 動態繫結
- Python - 動態型別
- Python - 抽象
- Python - 封裝
- Python - 介面
- Python - 包
- Python - 內部類
- Python - 匿名類和物件
- Python - 單例類
- Python - 包裝器類
- Python - 列舉
- Python - 反射
- Python 錯誤 & 異常
- Python - 語法錯誤
- Python - 異常
- Python - try-except 塊
- Python - try-finally 塊
- Python - 丟擲異常
- Python - 異常鏈
- Python - 巢狀 try 塊
- Python - 使用者自定義異常
- Python - 日誌記錄
- Python - 斷言
- Python - 內建異常
- Python 多執行緒
- Python - 多執行緒
- Python - 執行緒生命週期
- Python - 建立執行緒
- Python - 啟動執行緒
- Python - 連線執行緒
- Python - 執行緒命名
- Python - 執行緒排程
- Python - 執行緒池
- Python - 主執行緒
- Python - 執行緒優先順序
- Python - 守護執行緒
- Python - 執行緒同步
- Python 同步
- Python - 執行緒間通訊
- Python - 執行緒死鎖
- Python - 中斷執行緒
- Python 網路
- Python - 網路
- Python - Socket 程式設計
- Python - URL 處理
- Python - 泛型
- Python 庫
- NumPy 教程
- Pandas 教程
- SciPy 教程
- Matplotlib 教程
- Django 教程
- OpenCV 教程
- Python 雜項
- Python - 日期 & 時間
- Python - 數學
- Python - 迭代器
- Python - 生成器
- Python - 閉包
- Python - 裝飾器
- Python - 遞迴
- Python - 正則表示式
- Python - PIP
- Python - 資料庫訪問
- Python - 弱引用
- Python - 序列化
- Python - 模板
- Python - 輸出格式化
- Python - 效能測量
- Python - 資料壓縮
- Python - CGI 程式設計
- Python - XML 處理
- Python - GUI 程式設計
- Python - 命令列引數
- Python - 文件字串
- Python - JSON
- Python - 傳送郵件
- Python - 擴充套件
- Python - 工具/實用程式
- Python - GUIs
- Python 高階概念
- Python - 抽象基類
- Python - 自定義異常
- Python - 高階函式
- Python - 物件內部
- Python - 記憶體管理
- Python - 元類
- Python - 使用元類進行超程式設計
- Python - 模擬和存根
- Python - 猴子補丁
- Python - 訊號處理
- Python - 型別提示
- Python - 自動化教程
- Python - Humanize 包
- Python - 上下文管理器
- Python - 協程
- Python - 描述符
- Python - 診斷和修復記憶體洩漏
- Python - 不可變資料結構
- Python 有用資源
- Python - 問答
- Python - 線上測驗
- Python - 快速指南
- Python - 參考
- Python - 速查表
- Python - 專案
- Python - 有用資源
- Python - 討論
- Python 編譯器
- NumPy 編譯器
- Matplotlib 編譯器
- SciPy 編譯器
Python - 描述符
Python 描述符
Python 描述符 是一種自定義物件屬性的訪問、賦值和刪除的方式。它們提供了一種強大的機制,透過定義獲取、設定和刪除其值的方法來管理屬性的行為。描述符通常用於實現屬性、方法和屬性驗證。
描述符 是任何實現了至少一個方法(例如 __get__、__set__ 和 __delete__)的物件。這些方法控制如何訪問和修改屬性的值。
Python 描述符如何工作?
當在例項上訪問屬性時,Python 會在例項的類中查詢該屬性。如果找到該屬性並且它是一個描述符,則 Python 會呼叫相應的描述符方法,而不是簡單地返回屬性的值。這允許描述符控制在屬性訪問期間發生的事情。
描述符協議是一種低階機制,Python 中許多高階功能都使用它,例如屬性、方法、靜態方法和類方法。描述符可用於實現延遲載入、型別檢查和計算屬性等模式。
描述符方法
Python 描述符 包括三個主要方法,即 __get__()、__set__() 和 __delete__()。正如我們上面已經討論過的,這些方法分別控制屬性訪問、賦值和刪除的行為。
1. __get__() 方法
描述符中的 __get__() 方法 是 Python 中描述符協議的關鍵部分。它被呼叫以從例項或類中檢索屬性的值。瞭解 __get__() 方法 的工作原理對於建立可以以複雜方式管理屬性訪問的自定義描述符至關重要。
語法
以下是 Python 描述符 __get__ 方法 的語法:
def __get__(self, instance, owner): """ instance: the instance that the attribute is accessed through, or None when accessed through the owner class. owner: the owner class where the descriptor is defined. """
引數
以下是此方法的引數:
- self: 描述符例項。
- instance: 訪問屬性的類的例項。當透過類而不是例項訪問屬性時,它為 None。
- owner: 擁有描述符的類。
示例
以下是 __get__() 方法的基本示例,其中在訪問 obj.attr 時返回儲存的值 _value:
class Descriptor: def __get__(self, instance, owner): if instance is None:return self return instance._value class MyClass: attr = Descriptor() def __init__(self, value): self._value = value obj = MyClass(42) print(obj.attr)
輸出
42
2. __set__() 方法
__set__() 方法 是 Python 中描述符協議的一部分,用於控制設定屬性值的行為。當由描述符管理的屬性被賦予新值時,__set__() 方法 被呼叫,允許使用者自定義或強制執行賦值規則。
語法
以下是 Python 描述符 __set__() 方法 的語法:
def __set__(self, instance, value): """ instance: the instance of the class where the attribute is being set. value: the value to assign to the attribute. """
引數
以下是此方法的引數:
- self: 描述符例項。
- instance: 設定屬性的類的例項。
- value: 賦給屬性的值。
示例
以下是 __set__() 方法的基本示例,其中確保賦給 attr 的值是整數:
class Descriptor: def __set__(self, instance, value): if not isinstance(value, int): raise TypeError("Value must be an integer") instance._value = value class MyClass: attr = Descriptor() def __init__(self, value): self.attr = value obj = MyClass(42) print(obj.attr) obj.attr = 100 print(obj.attr)
輸出
<__main__.Descriptor object at 0x000001E5423ED3D0> <__main__.Descriptor object at 0x000001E5423ED3D0>
3. __delete__() 方法
描述符協議中的 __delete__() 方法 允許我們控制從例項中刪除屬性時發生的事情。當刪除屬性時,這對於管理資源、清理或強制執行約束很有用。
語法
以下是 Python 描述符 __delete__() 方法 的語法:
def __delete__(self, instance): """ instance: the instance of the class from which the attribute is being deleted. """
引數
以下是此方法的引數:
- self: 描述符例項。
- instance: 刪除屬性的類的例項。
示例
以下是 __set__() 方法的基本示例,其中確保賦給 attr 的值是整數:
class LoggedDescriptor: def __init__(self, name): self.name = name def __get__(self, instance, owner): return instance.__dict__.get(self.name) def __set__(self, instance, value): instance.__dict__[self.name] = value def __delete__(self, instance): if self.name in instance.__dict__: print(f"Deleting {self.name} from {instance}") del instance.__dict__[self.name] else: raise AttributeError(f"{self.name} not found") class Person: name = LoggedDescriptor("name") age = LoggedDescriptor("age") def __init__(self, name, age): self.name = name self.age = age # Example usage p = Person("Tutorialspoint", 30) print(p.name) print(p.age) del p.name print(p.name) del p.age print(p.age)
輸出
Tutorialspoint 30 Deleting name from <__main__.Person object at 0x0000021A1A67E2D0> None Deleting age from <__main__.Person object at 0x0000021A1A67E2D0> None
Python 描述符的型別
在 Python 中,描述符 可以根據它們實現的方法大致分為兩種型別。它們是:
- 資料描述符
- 非資料描述符
讓我們詳細瞭解這兩種 Python 描述符,以便我們更好地理解。
1. 資料描述符
資料描述符 是一種 Python 描述符,它定義了 __get__() 和 __set__() 方法。這些描述符優先於例項屬性,這意味著即使存在同名的例項屬性,也會始終呼叫描述符的 __get__() 和 __set__() 方法。
示例
下面是一個數據描述符的示例,它確保屬性始終為整數,並記錄訪問和修改操作。
class Integer: def __get__(self, instance, owner): print("Getting value") return instance._value def __set__(self, instance, value): print("Setting value") if not isinstance(value, int): raise TypeError("Value must be an integer") instance._value = value def __delete__(self, instance): print("Deleting value") del instance._value class MyClass: attr = Integer() # Usage obj = MyClass() obj.attr = 42 print(obj.attr) obj.attr = 100 print(obj.attr) del obj.attr
輸出
Setting value Getting value 42 Setting value Getting value 100 Deleting value
2. 非資料描述符
非資料描述符是 Python 中的一種描述符型別,它僅定義了__get__()方法。與資料描述符不同,非資料描述符可以被例項屬性覆蓋。這意味著如果存在同名的例項屬性,則它將優先於非資料描述符。
示例
以下是非資料描述符的一個示例,如果例項上未設定屬性,則它提供一個預設值。
class Default: def __init__(self, default): self.default = default def __get__(self, instance, owner): return getattr(instance, '_value', self.default) class MyClass: attr = Default("default_value") # Usage obj = MyClass() print(obj.attr) obj._value = "Tutorialspoint" print(obj.attr)
輸出
default_value Tutorialspoint
資料描述符與非資料描述符
瞭解 Python 描述符中資料描述符和非資料描述符之間的區別對於有效地利用它們的功能至關重要。
標準 | 資料描述符 | 非資料描述符 |
---|---|---|
定義 | 實現 __get__()、__set__() 方法,以及可選的 __delete__() 方法。 | 僅實現 __get__() 方法。 |
方法 | __get__(self, instance, owner) __set__(self, instance, value) __delete__(self, instance)(可選) |
__get__(self, instance, owner) |
優先順序 | 優先於例項屬性。 | 被例項屬性覆蓋。 |
用例 | 屬性驗證和強制執行, 託管屬性(例如,屬性), 記錄屬性訪問和修改, 強制執行只讀屬性。 |
方法繫結, 快取和, 提供預設值。 |
最後,我們可以說 Python 中的描述符提供了一種強大的機制來管理屬性的訪問和修改。瞭解資料描述符和非資料描述符之間的區別以及它們的適當用例對於建立健壯且可維護的 Python 程式碼至關重要。
透過利用描述符協議,開發人員可以實現高階行為,例如型別檢查、快取和只讀屬性。