Thoughts on PEP284

David Eppstein eppstein at ics.uci.edu
Tue Sep 23 21:26:56 CEST 2003


In article <mailman.1064341934.21653.python-list at python.org>,
 Michael Chermside <mcherm at mcherm.com> wrote:

> Stephen Horne writes:
> > 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.
> 
> I am of the opinion that the introduction of enumerate() (in 2.3) will
> go a long ways toward reducing the desire for better integer loop
> syntax. I'm sure it will never go away completely, but if the demand
> seems to die down after a year or so, then I'd guess it was due to
> enumerate().

It will certainly reduce the typical range(len(L)) verbosity.  It 
doesn't do anything to help the readability of expressions like 
range(dim-2,-1,-1) (example from some code I wrote a couple weeks ago).

Actually, looking at that code, the range is not the least readable part 
of it:

if dim == 1:
    positions = dict([(s, (positions[s][0],0)) for s in positions])
elif dim == 2:
    positions = dict([(s, (2*positions[s][0],2*positions[s][1]))
                      for s in positions])
elif not place3d():
    X = coordinate(range(1,dim))
    Y = coordinate(range(dim-2,-1,-1))
    positions = dict([(s, (X[s],Y[s])) for s in positions])

Dictionary comprehensions (PEP 274) would go a long way towards cleaning 
this up:

if dim == 1:
    positions = {s: (positions[s][0],0) for s in positions}
elif dim == 2:
    positions = {s: (2*positions[s][0],2*positions[s][1]))
                 for s in positions}
elif not place3d():
    X = coordinate(range(1,dim))
    Y = coordinate(range(dim-2,-1,-1))
    positions = {s: (X[s],Y[s]) for s in positions}

The use of PEP 284 syntax (replace the range by [i for dim-2 >= i >= 0])
would I think help but not as much.

-- 
David Eppstein                      http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science




More information about the Python-list mailing list