分布式属性 `(a+b)c = ac+bc` 也适用于 python 类型提示吗?

问题描述 投票:0回答:1

对于 Python ≤ 3.8:

from typing import Union, Tuple, List

Union[List[str], List[Tuple[str, str]]]

相同
List[Union[str, Tuple[str, str]]]
python python-3.x python-typing
1个回答
2
投票

它们在逻辑上并不相同。第一个是字符串列表或元组列表;第二个是可以包含字符串和元组的列表。

['alpha', ('beta', 'gamma')]
List[Union[str, Tuple[str, str]]]
有效,但与
Union[List[str], List[Tuple[str, str]]]
无效。

© www.soinside.com 2019 - 2024. All rights reserved.