[Python-ideas] Variable-length, homogeneous tuple: why? (was: Optional static typing -- the crossroads)

Steven D'Aprano steve at pearwood.info
Mon Aug 18 01:16:39 CEST 2014


On Sun, Aug 17, 2014 at 01:34:36PM -0700, Guido van Rossum wrote:

> Where is it said that Tuple[int] is a homogeneous variable size list?

Did you mean variable sized tuple rather than list? The mypy tutorial 
says the opposite:

http://www.mypy-lang.org/tutorial.html#tuples

which implies that Tuple[int] would accept (23,) but not (23, 42). 


There's two, or three, cases to consider:

Variable sized tuple of some homogenous type
- e.g. (1,), (1, 2)
- Tuple[int] for consistency with other types
- mypy doesn't appear to support this

Fixed size tuple of given hetrogeneous types
- e.g. (23, "abc"), (42, "xyz")
- mypy uses Tuple[int, str] which is a special case

Some way to specify two or more types, e.g. 
- (str, int) for consistency with isinstance, issubclass
- Union[str, int] as used by mypy
- str|int obvious short-cut for Union
- str*int will make type theorists happy


Making Tuple[] a special case troubles me, I strongly prefer Tuple[int] 
to mean a homogenous tuple of ints.

mypy already has Union[str, int] for union types (giving str|int as the 
obvious short-cut), which leaves (str, int) for the hetrogenous 2-tuple 
case.

Perhaps in 3.6 we can consider allowing isinstance and issubclass to 
accept Union types as well as tuples of types.


-- 
Steven


More information about the Python-ideas mailing list