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

Zero Piraeus schesis at gmail.com
Mon Aug 18 03:21:21 CEST 2014


:

On Sun, Aug 17, 2014 at 12:31:19PM -0700, Andrew Barnert wrote:
> I think we're conflating multiple problems here.
> 
> Sometimes we use tuple to mean a homogeneous, arbitrary-length,
> immutable sequence, and other times we use it to mean a heterogeneous,
> fixed-length sequence. Nick's list demonstrates that the former is (a)
> common enough to worry about, and (b) not always a mistake.
> 
> [...]
> 
> I can think of four possibilities:

Since we're already repurposing a number of operators - TypeA[TypeB],
TypeA|TypeB and possibly TypeA&TypeB, here's a fifth possibility:

Leave Tuple[int] with the same semantic meaning as List[int], Set[int]
etc., and use e.g. Tuple%(int, str, float) to declare the signature of a
fixed-length, heterogenous sequence (borrowing from the use of % in
string interpolation).

This has the advantages that it 

a) doesn't make tuple a special case, and so allows other container
types to use it (including user-defined types which aren't tuples, but
are intended to behave similarly to them), and

b) is both reasonably compact, and easily distinguished from Tuple[int].

The specific operator used doesn't really matter - when this idea came
to me I thought of @ (since it's shiny and new in 3.5), but % seems a
better conceptual fit.

 -[]z.

-- 
Zero Piraeus: unus multorum
http://etiol.net/pubkey.asc


More information about the Python-ideas mailing list