
- 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 - 套接字程式設計
- 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 的型別提示在 PEP 484 中引入,旨在將靜態型別的優勢帶入動態型別語言。雖然型別提示不會在執行時強制執行型別檢查,但它們提供了一種指定變數、函式引數和返回值的預期型別的方法,這些型別可以透過靜態分析工具(例如mypy)進行檢查。這增強了程式碼的可讀性,方便了除錯,並提高了程式碼的整體可維護性。
Python 中的型別提示使用註解來指定函式引數、返回值和變數賦值。
Python 的型別提示可用於指定各種型別,例如基本資料型別、集合、複雜型別和自定義使用者定義型別。typing 模組提供了許多內建型別來表示這些各種型別:
讓我們逐一詳細瞭解每一個。
基本資料型別
在 Python 中,當使用型別提示指定基本型別時,我們可以簡單地使用型別的名稱作為註解。
示例
以下是使用基本資料型別(例如整數、浮點數、字串等)的示例:
from typing import Optional # Integer type def calculate_square_area(side_length: int) -> int: return side_length ** 2 # Float type def calculate_circle_area(radius: float) -> float: return 3.14 * radius * radius # String type def greet(name: str) -> str: return f"Hello, {name}" # Boolean type def is_adult(age: int) -> bool: return age >= 18 # None type def no_return_example() -> None: print("This function does not return anything") # Optional type (Union of int or None) def safe_divide(x: int, y: Optional[int]) -> Optional[float]: if y is None or y == 0: return None else: return x / y # Example usage print(calculate_square_area(5)) print(calculate_circle_area(3.0)) print(greet("Alice")) print(is_adult(22)) no_return_example() print(safe_divide(10, 2)) print(safe_divide(10, 0)) print(safe_divide(10, None))
執行上述程式碼後,我們將得到以下輸出:
25 28.259999999999998 Hello, Alice True This function does not return anything 5.0 None None
集合型別
在 Python 中,處理列表、元組、字典等集合時,在型別提示中,我們通常使用typing 模組來指定集合型別。
示例
以下是型別提示中使用集合的示例:
from typing import List, Tuple, Dict, Set, Iterable, Generator # List of integers def process_numbers(numbers: List[int]) -> List[int]: return [num * 2 for num in numbers] # Tuple of floats def coordinates() -> Tuple[float, float]: return (3.0, 4.0) # Dictionary with string keys and integer values def frequency_count(items: List[str]) -> Dict[str, int]: freq = {} for item in items: freq[item] = freq.get(item, 0) + 1 return freq # Set of unique characters in a string def unique_characters(word: str) -> Set[str]: return set(word) # Iterable of integers def print_items(items: Iterable[int]) -> None: for item in items: print(item) # Generator yielding squares of integers up to n def squares(n: int) -> Generator[int, None, None]: for i in range(n): yield i * i # Example usage numbers = [1, 2, 3, 4, 5] print(process_numbers(numbers)) print(coordinates()) items = ["apple", "banana", "apple", "orange"] print(frequency_count(items)) word = "hello" print(unique_characters(word)) print_items(range(5)) gen = squares(5) print(list(gen))
執行上述程式碼後,我們將得到以下輸出:
[2, 4, 6, 8, 10] (3.0, 4.0) {'apple': 2, 'banana': 1, 'orange': 1} {'l', 'e', 'h', 'o'} 0 1 2 3 4 [0, 1, 4, 9, 16]
可選型別
在 Python 中,可選型別用於指示變數可以是指定型別或 None。當函式可能並非總是返回值,或者引數可以接受值或留空時,這尤其有用。
示例
以下是型別提示中使用可選型別的示例:
from typing import Optional def divide(a: float, b: float) -> Optional[float]: if b == 0: return None else: return a / b result1: Optional[float] = divide(10.0, 2.0) # result1 will be 5.0 result2: Optional[float] = divide(10.0, 0.0) # result2 will be None print(result1) print(result2)
執行上述程式碼後,我們將得到以下輸出:
5.0 None
聯合型別
Python 使用聯合型別允許變數接受不同型別的值。當函式或資料結構可以使用各種型別的輸入或產生不同型別的輸出時,這很有用。
示例
以下是這個示例:
from typing import Union def square_root_or_none(number: Union[int, float]) -> Union[float, None]: if number >= 0: return number ** 0.5 else: return None result1: Union[float, None] = square_root_or_none(50) result2: Union[float, None] = square_root_or_none(-50) print(result1) print(result2)
執行上述程式碼後,我們將得到以下輸出:
7.0710678118654755 None
Any 型別
在 Python 中,Any 型別是一種特殊的型別提示,它表示變數可以是任何型別。它實際上停用了該特定變數或表示式的型別檢查。當事先不知道值的型別或處理動態資料時,這可能很有用。
示例
以下是型別提示中使用 Any 型別的示例:
from typing import Any def print_value(value: Any) -> None: print(value) print_value(10) print_value("hello") print_value(True) print_value([1, 2, 3]) print_value({'key': 'value'})
執行上述程式碼後,我們將得到以下輸出:
10 hello True [1, 2, 3] {'key': 'value'}
類型別名
Python 中的類型別名用於為現有型別提供替代名稱。它們可以透過為複雜的型別註解或型別的組合提供清晰的名稱來使程式碼更易於閱讀。這在處理巢狀結構或長型別提示時尤其有用。
示例
以下是型別提示中使用類型別名的示例:
from typing import List, Tuple # Define a type alias for a list of integers Vector = List[int] # Define a type alias for a tuple of coordinates Coordinates = Tuple[float, float] # Function using the type aliases def scale_vector(vector: Vector, factor: float) -> Vector: return [int(num * factor) for num in vector] def calculate_distance(coord1: Coordinates, coord2: Coordinates) -> float: x1, y1 = coord1 x2, y2 = coord2 return ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5 # Using the type aliases v: Vector = [1, 2, 3, 4] scaled_v: Vector = scale_vector(v, 2.5) print(scaled_v) c1: Coordinates = (3.0, 4.0) c2: Coordinates = (6.0, 8.0) distance: float = calculate_distance(c1, c2) print(distance)
執行上述程式碼後,我們將得到以下輸出:
[2, 5, 7, 10] 5.0
泛型型別
泛型 建立可以處理任何型別同時保持型別安全性的函式、類或資料結構。typing 模組的 TypeVar 和 Generic 構造使得這成為可能。它們有助於建立可重用的元件,這些元件可以與各種型別一起工作,而不會影響型別檢查。
示例
這是一個示例:
from typing import TypeVar, List # Define a type variable T T = TypeVar('T') # Generic function that returns the first element of a list def first_element(items: List[T]) -> T: return items[0] # Example usage int_list = [1, 2, 3, 4, 5] str_list = ["apple", "banana", "cherry"] first_int = first_element(int_list) # first_int will be of type int first_str = first_element(str_list) # first_str will be of type str print(first_int) print(first_str)
執行上述程式碼後,我們將得到以下輸出:
1 apple
可呼叫型別
Python 的Callable 型別用於表明某種型別是函式或可呼叫物件。它位於 typing 模組中,允許你定義函式的引數型別和返回型別。這對於高階函式非常有用。
示例
以下是使用 Callable 型別進行型別提示的示例:
from typing import Callable # Define a function that takes another function as an argument def apply_operation(x: int, y: int, operation: Callable[[int, int], int]) -> int: return operation(x, y) # Example functions to be passed as arguments def add(a: int, b: int) -> int: return a + b def multiply(a: int, b: int) -> int: return a * b # Using the apply_operation function with different operations result1 = apply_operation(5, 3, add) # result1 will be 8 result2 = apply_operation(5, 3, multiply) # result2 will be 15 print(result1) print(result2)
執行上述程式碼後,我們將得到以下輸出:
8 15
字面量型別
Literal 型別用於指定值必須是預定義值集合中的一個。
示例
以下是一個示例:
from typing import Literal def move(direction: Literal["left", "right", "up", "down"]) -> None: print(f"Moving {direction}") move("left") # Valid move("up") # Valid
執行上述程式碼後,我們將得到以下輸出:
Moving left Moving up
NewType
NewType 是 typing 模組中的一個函式,它允許我們建立從現有型別派生的 distinct 型別。這對於透過區分相同底層型別的不同用途來增強程式碼型別安全性非常有用。例如,我們可能希望區分使用者 ID 和產品 ID,即使兩者都表示為整數。
示例
以下是一個示例:
from typing import NewType # Create new types UserId = NewType('UserId', int) ProductId = NewType('ProductId', int) # Define functions that use the new types def get_user_name(user_id: UserId) -> str: return f"User with ID {user_id}" def get_product_name(product_id: ProductId) -> str: return f"Product with ID {product_id}" # Example usage user_id = UserId(42) product_id = ProductId(101) print(get_user_name(user_id)) # Output: User with ID 42 print(get_product_name(product_id)) # Output: Product with ID 101
執行上述程式碼後,我們將得到以下輸出:
User with ID 42 Product with ID 101