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

kj no.email at please.post
Sat Jun 13 19:01:04 EDT 2009


In <h113qm$pl2$1 at reader1.panix.com> kj <no.email at please.post> 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
well.

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

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

  foo[:]

is intrinsically clearer, or easier to read than

  foo[0:len(foo)]

?

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[:].

kj




More information about the Python-list mailing list