
- 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 - Monkey Patching
- 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 中的反射函式列表:
- type() 函式
- isinstance() 函式
- issubclass() 函式
- callable() 函式
- getattr() 函式
- setattr() 函式
- hasattr() 函式
- dir() 函式
type() 函式
我們多次使用過此函式。它告訴您物件屬於哪個類。
示例
以下語句列印不同內建資料型別物件的相應類
print (type(10)) print (type(2.56)) print (type(2+3j)) print (type("Hello World")) print (type([1,2,3])) print (type({1:'one', 2:'two'}))
在這裡,您將獲得以下輸出:
<class 'int'> <class 'float'> <class 'complex'> <class 'str'> <class 'list'> <class 'dict'>
讓我們驗證使用者定義類物件的型別:
class test: pass obj = test() print (type(obj))
它將產生以下輸出:
<class '__main__.test'>
isinstance() 函式
這是 Python 中的另一個內建函式,它確定物件是否是給定類的例項。
語法
isinstance(obj, class)
此函式始終返回布林值,如果物件確實屬於給定類,則返回 true,否則返回 false。
示例
以下語句返回 True:
print (isinstance(10, int)) print (isinstance(2.56, float)) print (isinstance(2+3j, complex)) print (isinstance("Hello World", str))
它將產生以下輸出:
True True True True
相反,這些語句列印 False。
print (isinstance([1,2,3], tuple)) print (isinstance({1:'one', 2:'two'}, set))
它將產生以下輸出:
False False
您還可以使用使用者定義的類進行檢查
class test: pass obj = test() print (isinstance(obj, test))
它將產生以下輸出:
True
在 Python 中,即使類也是物件。所有類都是 object 類的物件。可以透過以下程式碼驗證:
class test: pass print (isinstance(int, object)) print (isinstance(str, object)) print (isinstance(test, object))
以上所有 print 語句都列印 True。
issubclass() 函式
此函式檢查一個類是否是另一個類的子類。與類有關,而不是它們的例項。
如前所述,所有 Python 類都是從 object 類繼承的。因此,以下 print 語句的輸出對所有語句都為 True。
class test: pass print (issubclass(int, object)) print (issubclass(str, object)) print (issubclass(test, object))
它將產生以下輸出:
True True True
callable() 函式
如果物件呼叫某個過程,則該物件是可呼叫的。Python 函式執行某個過程,是一個可呼叫物件。因此 callable(function) 返回 True。任何函式(內建的、使用者定義的或方法)都是可呼叫的。內建資料型別(如 int、str 等)的物件不可呼叫。
示例
def test(): pass print (callable("Hello")) print (callable(abs)) print (callable(list.clear([1,2]))) print (callable(test))
字串物件不可呼叫。但 abs 是一個可呼叫的函式。list 的 pop 方法是可呼叫的,但clear()實際上是對函式的呼叫,而不是函式物件,因此不可呼叫。
它將產生以下輸出:
False True True False True
如果一個類的例項具有__call__()方法,則它是可呼叫的。在下面的示例中,test 類包含__call__()方法。因此,它的物件可以像呼叫函式一樣使用。因此,具有__call__()方法的類的物件是可呼叫的。
class test: def __init__(self): pass def __call__(self): print ("Hello") obj = test() obj() print ("obj is callable?", callable(obj))
它將產生以下輸出:
Hello obj is callable? True
getattr() 函式
內建函式getattr()檢索物件的命名屬性的值。
示例
class test: def __init__(self): self.name = "Manav" obj = test() print (getattr(obj, "name"))
它將產生以下輸出:
Manav
setattr() 函式
內建函式setattr()向物件新增一個新屬性併為其賦值。它也可以更改現有屬性的值。
在下面的示例中,test 類的物件只有一個屬性 - name。我們使用setattr()新增 age 屬性並修改 name 屬性的值。
class test: def __init__(self): self.name = "Manav" obj = test() setattr(obj, "age", 20) setattr(obj, "name", "Madhav") print (obj.name, obj.age)
它將產生以下輸出:
Madhav 20
hasattr() 函式
此內建函式如果給定的屬性可用於物件引數,則返回 True,否則返回 false。我們使用相同的 test 類並檢查它是否具有某個屬性。
class test: def __init__(self): self.name = "Manav" obj = test() print (hasattr(obj, "age")) print (hasattr(obj, "name"))
它將產生以下輸出:
False True
dir() 函式
如果此內建函式不帶引數呼叫,則返回當前作用域中的名稱。對於任何作為引數的物件,它都會返回給定物件及其可訪問屬性的列表。
對於模組物件 - 該函式返回模組的屬性。
對於類物件 - 該函式返回其屬性,並遞迴返回其基類的屬性。
對於任何其他物件 - 其屬性、其類的屬性及其類基類的屬性(遞迴)。
示例
print ("dir(int):", dir(int))
它將產生以下輸出:
dir(int): ['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__getstate__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'as_integer_ratio', 'bit_count', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']
示例
print ("dir(dict):", dir(dict))
它將產生以下輸出:
dir(dict): ['__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__ior__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__ror__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
示例
class test: def __init__(self): self.name = "Manav" obj = test() print ("dir(obj):", dir(obj))
它將產生以下輸出:
dir(obj): ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'name']