【发布时间】:2021-05-16 02:01:18
【问题描述】:
from typing import Tuple, TypeVar, Any
ParamArray = TypeVar("ParamArray", Tuple[Any, ...])
这个概念是; ParamArray 只是一个值的元组。我有一个功能
def integrate(func, a, b, args=()):
delta = 0.1
running_total = 0.
for x in range(a, b, step=delta):
running_total += func(x, args) * delta
return running_total
因此我试图输入它:
def integrate(func:Callable[[float, ParamArray], float], a: float, b: float, args: ParamArray=()) -> float:
尝试传达 args ParamArray 与传递给 func 的事实相同,因此回调必须能够接受传递的任何参数。
我遇到了一些错误
TypeVar cannot have only a single constraint Argument 2 to "TypeVar" has incompatible type "object"; expected "Type[Any]" Variable "typealiases.ParamArray" is not valid as a type
【问题讨论】:
-
您是否尝试过使用函数:collections.namedtuple() 完成您的任务?
标签: python mypy python-typing