Range Operation pre-PEP
Douglas Alan
nessus at mit.edu
Wed May 9 23:33:26 EDT 2001
Ben Hutchings <ben.hutchings at roundpoint.com> writes:
> Douglas Alan <nessus at mit.edu> writes:
> > Speaking of creeping-featurism, how come we have list comprehension,
> > but not tuple comprehension? Seems inconsistent to me.
> A list contains a variable number of homogeneous values, e.g. the
> lines of a file. Lists are like arrays in other languages. A tuple
> contains a fixed number of heterogeneous values where each element
> has a distinct meaning e.g. (year, month, day) for a date.
I think you have some other language in mind, rather than Python. In
Python, the only semantic difference between a list and a tuple is
that a tuple is immutable.
> Tuples are like data structures or product types in other languages,
> except that their types and fields are nameless. Comprehensions
> work with a variable number of homogeneous values, so they produce
> lists.
filter() on a tuple returns a tuple. The length of the tuple cannot
be known in advance. map(), on the other hand, always returns a list.
But map can take multiple sequence arguments, so it wouldn't always be
obvious to know which type of sequence to duplicate. I suppose the
same is true for comprehension. But, it seems to me that Python could
allow
(x*2 for x in t)
to make a tuple as easily as it allows
[x*2 for x in l]
to make a list.
|>oug
More information about the Python-list
mailing list