[Python-ideas] Variable-length, homogeneous tuple: why?

Ben Finney ben+python at benfinney.id.au
Sun Aug 17 12:53:18 CEST 2014


Terry Reedy <tjreedy at udel.edu> writes:

> On 8/17/2014 4:23 AM, Ben Finney wrote:
> > Guido van Rossum <guido at python.org> writes:
> >
> >> A mostly unrelated issue: there are two different uses of tuples, and
> >> we need a notation for both. One is a tuple of fixed length with
> >> heterogeneous, specific types for the elements; for example Tuple[int,
> >> float].
> >
> > That's the meaning of a tuple data structure, to me.
> >
> >> But I think we also need a way to indicate that a function expects
> >> (or returns) a variable-length tuple with a homogeneous element
> >> type.
>
> There are also fixed-length homogeneous structures, like points.

I assume you mean where the sequence is something like ‘(x, y, z)’ where
each position has a meaning specific to that position.

That's not homogeneous as I understood this usage, because the psitions
are not homogeneous in meaning. The number 7.03 has very different
meaning in the first, second, or third positions.

A homogeneous sequence would imply there are deliberately *no* specific
meanings to each position. The value 7.03 would have the same semantic
value at any position in the sequence.

So a fixed-length sequence where each position implies a special meaning
is a heterogeneous sequence, and I agree that's an excellent use for a
tuple.

> > Why? What real-world uses are there, where a list won't do the job
> > adequately?
>
> Variable-length homogenous tuples are part of python syntax in
> multiple places.
>
> Tuples can be hashed and put in sets an used as dict keys, lists
> cannot. Tuple contants are calculated just once when the code is
> compiled (and typically saved as .pyc).

Okay, so these are not for the semantic purpose a tuple is for. I think
a putative “frozenlist” type is best for that, to keep the heterogeneous
implication of a tuple separate from the homogeneous implication of a
list.

-- 
 \           “I prayed for twenty years but received no answer until I |
  `\          prayed with my legs.” —Frederick Douglass, escaped slave |
_o__)                                                                  |
Ben Finney



More information about the Python-ideas mailing list