小编pdo*_*ing的帖子

Python 3.6中的通用NamedTuple

我正在尝试创建NamedTuple的通用版本,如下所示:

T1 = TypeVar("T1")
T2 = TypeVar("T2")

class Group(NamedTuple, Generic[T1, T2]):
    key: T1
    group: List[T2]

g = Group(1, [""])  # expecting type to be Group[int, str]
Run Code Online (Sandbox Code Playgroud)

但是,出现以下错误:

TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
Run Code Online (Sandbox Code Playgroud)

我不确定在这里还能做什么,或者在某种程度上这可能是键入机制中的错误。

python generics typing python-3.6

4
推荐指数
1
解决办法
943
查看次数

标签 统计

generics ×1

python ×1

python-3.6 ×1

typing ×1