我正在尝试在 Python 3.7(和 3.8)中使用通用命名元组功能,但解释器会引发错误。我用的是坏方法吗?
from typing import NamedTuple, TypeVar, Generic
from dataclasses import dataclass
@dataclass
class Person:
name: str
age: int
T = TypeVar("T")
class MyResult(NamedTuple, Generic[T]):
Body: T
Status: int
def func1() -> MyResult[Person]:
return MyResult(Person('asghar',12), 200)
引发以下错误:
Traceback (most recent call last):
File "/Users/kamyar/Documents/generic_named_tuple.py", line 16, in <module>
def func1() -> MyResult[Type[Person]]:
TypeError: 'type' object is not subscriptable