Python打字以返回多个参数类型变量的元组
我已经有一个函数,该函数根据其参数收到的类型返回值:
T = TypeVar('T', int, str)
def get(t: Type[T]) -> T: ...
因此,get(int)
返回int
和get(str)
返回str
。
我还有一个版本的get
,该版本用于将许多值作为元组获取:
def get_many(*ts: Type[T]):
return tuple(get(t) for t in ts)
如何注释get_many
的返回类型?
需要清楚,get_many(int,str)
应返回tuple [int,str]
,get_many(str,str,str,int)
应返回元组[str,str,int]
等。
I already have a function that returns a value according to the type it receives as its argument:
T = TypeVar('T', int, str)
def get(t: Type[T]) -> T: ...
So that get(int)
returns an int
and get(str)
returns a str
.
I also have a version of get
that is used to get many values as a tuple:
def get_many(*ts: Type[T]):
return tuple(get(t) for t in ts)
How should the return type of get_many
be annotated?
To be clear, get_many(int, str)
should return tuple[int, str]
, get_many(str, str, int)
should return tuple[str, str, int]
and so on.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论