Home > Back-end >  Does distributive property `(a b)c = ac bc` apply to python type hinting as well?
Does distributive property `(a b)c = ac bc` apply to python type hinting as well?

Time:04-21

For Python ≤ 3.8:

from typing import Union, Tuple, List

Is

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

the same as

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

CodePudding user response:

They are not logically the same. The first is either a list of strings or a list of tuples; the second is a list that can contain strings and tuples.

['alpha', ('beta', 'gamma')] is valid as List[Union[str, Tuple[str, str]]], but not valid as Union[List[str], List[Tuple[str, str]]].

  • Related