Perl's @foo[3,7,1,-1] ?

kj at
Sat Jun 13 19:01:04 EDT 2009

In <h113qm$pl2$1 at> kj < at> writes:

>OK, I see: if Python allowed foo[3,7,1,-1], then foo[3] would be
>ambiguous: does it mean the fourth element of foo, or the tuple
>consisting of this element alone?  I suppose that's good enough
>reason to veto this idea...

Hmmm, come to think of it, this argument is weaker than I thought
at first.  Python already has cases where it has to deal with this
sort of ambiguity, and does so with a trailing comma.  So the two
cases could be disambiguated: foo[3] for the single element, and
foo[3,] for the one-element tuple.

Also, the association of this idiom with Perl is a bit unfair:
tuple-index is very common in other languages, and in pure math as

As I said in my original post, Perl code often has very obscure
expressions, but I would never describe tuple indexing as one of

By the same token, the desing of Python does not entirely disregard
considerations of ease of writing.  Who could argue that


is intrinsically clearer, or easier to read than



Please don't misunderstand me here.  I don't want to critize, let
alone change, Python.  I'm sure there is a good reason for why
Python doesn't support foo[3,7,1,-1], but I have not figured it
out yet.  I still find it unconvincing that it would be for the
sake of keeping the code easy to read, because I don't see how
foo[3,7,1,-1] is any more confusing than foo[:].


More information about the Python-list mailing list