PEP 276 Simple Iterator for ints

Ken Seehof kseehof at neuralintegrator.com
Thu Nov 15 12:42:56 EST 2001


> David Eppstein wrote:
> >    for i in [n-1, n-2, ... 0]:
> >        for j in [i+1, i+2, ... n]:
> >            ...do something...
>
> James_Althoff at i2.com wrote:
> |Thanks for your interesting example.  I agree that your notation "[n-1,
> |n-2, ... 0]" is a very nice way of showing a sequence of integers.  I'm
not
> |sure how one would turn this into programming language syntax that would
be
> |as clear.
>
> Well... here's how Haskell does it:
>
>   [1 .. 4]         --> [1,2,3,4]
>   [4,3 .. 0]       --> [4,3,2,1,0]
>   [2,4 .. 10]      --> [2,4,6,8,10]
>   [0.0,0.3 .. 1.0] --> [0.0,0.3,0.6,0.9]
>
> It's hard to think of a better syntax for Python to use.

I don't have a problem with the Haskell syntax, except that is is
inconsistent
with the way slicing is done in python everywhere else.  Adding new syntax
to python is generally bad.  Adding new/inconsistent semantics is also
generally bad (beginners will be confused by conflicting idioms, even if the
new idiom is supposedly "better").  There isn't anything in the Haskell
syntax
that doesn't map onto pythons slicing syntax, therefore we should use python
syntax and semantics if possible.

- Ken






More information about the Python-list mailing list