【发布时间】:2021-11-28 20:29:25
【问题描述】:
当我使用 Union of types 时如何具体说明,假设我编写了一个函数,它接受 str 或 list 并输出传入的任何类型,请考虑以下示例
from typing import Union
def concat(x1: Union[str, list], x2: Union[str, list]) -> Union[str, list]:
return x1 + x2
我希望上面的示例更具体,例如 x1、x2,并且函数的返回值都必须匹配相同的类型,但这些类型必须是 str 或 list。
【问题讨论】:
标签: python type-hinting python-typing