![](/img/trans.png)
[英]Python Typing: TypedDict - What's the difference between TypedDict instance and TypedDict Class?
[英]Variadic generic type for python's TypedDict?
我想做類似的事情:
from typing import TypeVar, Generic, TypedDict
T = TypeVar("T")
class Foo(Generic[T], TypedDict):
bar: T
...
foo: Foo[int] = {"bar": 42}
但這會產生類型錯誤(“不能從 TypedDict 和非 TypedDict 基類繼承”)。
有沒有辦法達到這個結果?
目前這是不可能的。 您不能同時繼承 TypedDict 和非 TypedDict 基礎 class 。 但是cpython 存儲庫中存在活動問題,並且正在進行支持此功能的討論。
編輯:根據最新更新,此功能將從 python 3.11 開始提供。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.