Thoughts on PEP284

Stephen Horne $$$$$$$$$$$$$$$$$ at $$$$$$$$$$$$$$$$$$$$.co.uk
Tue Sep 23 06:49:14 CEST 2003


On Mon, 22 Sep 2003 21:00:32 -0700, David Eppstein
<eppstein at ics.uci.edu> wrote:

>"for i in 0:10:2:" would be more Pythonic, but has a 
>little ambiguity problem ("for i in 0:10:2" without the colon seems like 
>it should execute the statement "2" ten times) and again doesn't add 
>much in readability or power to what we can already do with range.

That's pretty close to my suggestion, which - in brief - was...

  for i in int [0:10:2] :
    ...

that is, allow the 'int' type object to be sliced, and return an
iterator as the result - not just for the for loop, but generally
(though optimising the 'for' loop usage to avoid physically creating
the iterator might be a good idea).

This shouldn't be too wierd as Pythonistas are already familiar with
slicing - it's just that a type is being sliced rather than a list or
string. But to anyone familiar with computer science, a set of values
is a key feature of any abstract data type anyway. It just so happens
that the set of integers is conveniently sliceable (as long as the
start value is specified).

True, it is mostly just an alternative notation for range or xrange.
But the integer for loops thing is something that never seems to go
away. PEP284 itself quotes four previous PEPs on broadly the same
issue.

The rejected PEP204 is uncomfortably close to my (and your)
suggestions, but while the difference is small I think it is
significant. It is reasonable to think of a type as a potentially
sliceable set of values, and practical to implement providing the type
has certain properties such as being discrete.

Also, one extra feature is that the loop can be infinite (which range
and xrange cannot achieve)...

  for i in int [0:] :
    ...
    if condition : break
    ...


-- 
Steve Horne

steve at ninereeds dot fsnet dot co dot uk




More information about the Python-list mailing list