小智 6
Python类型提示不支持排除类型,但是,您可以使用Union类型来指定您想要获取的类型。
所以像这样:
def x(x: Iterable[Union[int, str, dict]]):
pass
x([1]) # correct
x([1, ""]) # correct
x([None]) # not correct
Run Code Online (Sandbox Code Playgroud)
Union[]如果你想获得除你可以做的事情之外的所有类型,一种缩短的方法:
expected_types = Union[int, str, dict]
def x(x: Iterable[expected_types]):
pass
Run Code Online (Sandbox Code Playgroud)
这就像上面的代码一样工作。