What's the difference between tuple[str, ...] vs tuple[str]? (python typing)
giving a tuple type with ellipsis means that number of elements in this tuple is not known, but type is known:
x: tuple[str, ...] = ("hi",) #VALID
x: tuple[str, ...] = ("hi", "world") #ALSO VALID
not using ellipsis means a tuple with specific number of elements, e.g.:
y: tuple[str] = ("hi", "world") # Type Warning: Expected type 'Tuple[str]', got 'Tuple[str, str]' instead
This goes in contrast to notation of other collections, e.g. list[str] means list of any length with elements of type str.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With