- FastAPI 教程
- FastAPI - 首頁
- FastAPI - 簡介
- FastAPI - Hello World
- FastAPI - OpenAPI
- FastAPI - Uvicorn
- FastAPI - 型別提示
- FastAPI - IDE 支援
- FastAPI - REST 架構
- FastAPI - 路徑引數
- FastAPI - 查詢引數
- FastAPI - 引數驗證
- FastAPI - Pydantic
- FastAPI - 請求體
- FastAPI - 模板
- FastAPI - 靜態檔案
- FastAPI - HTML 表單模板
- FastAPI - 訪問表單資料
- FastAPI - 上傳檔案
- FastAPI - Cookie 引數
- FastAPI - 頭部引數
- FastAPI - 響應模型
- FastAPI - 巢狀模型
- FastAPI - 依賴項
- FastAPI - CORS
- FastAPI - CRUD 操作
- FastAPI - SQL 資料庫
- FastAPI - 使用 MongoDB
- FastAPI - 使用 GraphQL
- FastAPI - Websockets
- FastAPI - FastAPI 事件處理器
- FastAPI - 掛載子應用
- FastAPI - 中介軟體
- FastAPI - 掛載 Flask 應用
- FastAPI - 部署
- FastAPI 有用資源
- FastAPI - 快速指南
- FastAPI - 有用資源
- FastAPI - 討論
FastAPI - 型別提示
FastAPI 廣泛使用了 Python 3.5 及更高版本提供的型別提示功能。事實上,Python 以動態型別語言而聞名,這也是 Python 的一個顯著特徵。在 Python 程式碼中,無需宣告變數屬於某種型別,其型別由動態賦予它的值決定。Python 直譯器不執行型別檢查,因此容易出現執行時異常。
在下面的例子中,定義了一個division()函式,它有兩個引數,並返回它們的商,假設引數是數字。
>>> def division(a, b): return a/b >>> division(10, 4) 2.5 >>> division(10, 2.5) 4.0
但是,如果傳遞給函式的值之一是非數字,則會產生 TypeError,如下所示:
>>> division("Python",5)
TypeError: unsupported operand type(s) for /: 'str' and 'int'
即使是像 IDLE 這樣的基本編碼環境也會指出該函式需要兩個引數,但不會指定型別,因為它們沒有被宣告。
Python 的新型別提示功能有助於提示使用者傳遞的引數的預期型別。這是透過在引數之後新增冒號和資料型別來實現的。我們將重新定義 division() 函式如下:
注意,在呼叫函式時,Python 會提示每個引數的預期型別。但是,如果傳遞了不相容的值,這並不會阻止 TypeError 的出現。您必須使用靜態型別檢查器,例如MyPy,在執行之前檢查相容性。
就像函式定義中的形式引數一樣,可以為函式的返回值提供型別提示。在函式定義語句的冒號符號之前(函式塊開始之後),新增箭頭 (->) 和型別。
但是,如前所述,如果傳遞給函式的值不相容,或者函式返回的值不相容,Python 會報告 TypeError。使用 MyPy 靜態型別檢查器可以檢測此類錯誤。請先安裝 mypy 包。
pip3 install mypy
將以下程式碼儲存為 typecheck.py
def division(x:int, y:int) -> int:
return (x//y)
a=division(10,2)
print (a)
b=division(5,2.5)
print (b)
c=division("Hello",10)
print (c)
使用 mypy 檢查此程式碼的型別錯誤。
C:\python37>mypy typechk.py typechk.py:7: error: Argument 2 to "division" has incompatible type "float"; expected "int" typechk.py:10: error: Argument 1 to "division" has incompatible type "str"; expected "int" Found 2 errors in 1 file (checked 1 source file)
函式的第二次和第三次呼叫中存在錯誤。在第二次呼叫中,傳遞給y的值是float,而預期的是int。在第三次呼叫中,傳遞給x的值是str,而預期的是int。(注意 // 運算子返回整數除法)
所有標準資料型別都可以用作型別提示。這可以在全域性變數、函式引數變數以及函式定義內部等地方進行。
x: int = 3
y: float = 3.14
nm: str = 'abc'
married: bool = False
names: list = ['a', 'b', 'c']
marks: tuple = (10, 20, 30)
marklist: dict = {'a': 10, 'b': 20, 'c': 30}
Python 新版本(3.5 及更高版本)標準庫中的一個新增內容是 typing 模組。它為相應的標準集合型別定義了特殊型別。typing 模組中的型別是List、Tuple、Dict 和 Sequence。它還包含Union和Optional型別。請注意,資料型別的標準名稱都是小寫,而 typing 模組中的名稱首字母大寫。使用此功能,我們可以要求一個特定型別的集合。
from typing import List, Tuple, Dict
# following line declares a List object of strings.
# If violated, mypy shows error
cities: List[str] = ['Mumbai', 'Delhi', 'Chennai']
# This is Tuple with three elements respectively
# of str, int and float type)
employee: Tuple[str, int, float] = ('Ravi', 25, 35000)
# Similarly in the following Dict, the object key should be str
# and value should be of int type, failing which
# static type checker throws error
marklist: Dict[str, int] = {'Ravi': 61, 'Anil': 72}