简体   繁体   中英

Python typing: tuple[str] vs tuple[str, ...]

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, eg:

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, eg list[str] means list of any length with elements of type str .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM