PEP 276 Simple Iterator for ints
David Eppstein
eppstein at ics.uci.edu
Tue Nov 13 17:22:50 EST 2001
As a possibly-related side note to this debate:
I have been moving towards using Python-like pseudocode in my algorithms
classes in place of the C/C++/Java-like pseudocode I was previously using;
that is, using colons and indentation to mark blocks of code instead of
curly braces, etc. As well as conciseness, Python offers some other
advantages e.g. in the ability to write code with list comprehensions.
The single biggest barrier for me to write actual working Python code is
the range/xrange syntax: I just don't feel comfortable writing code like
for i in range(n-1,-1,-1):
for j in range(i+1,n+1):
...do something...
because I don't expect my students (who are not required to learn Python)
to understand from that syntax that the outer loop runs backwards from n-1
to 0 and that the inner loop runs from i+1 to n.
Instead I have to make up some non-Python syntax which is less formal but
immediately clear:
for i in [n-1, n-2, ... 0]:
for j in [i+1, i+2, ... n]:
...do something...
(actual example from <http://www.ics.uci.edu/~eppstein/260/011023/>).
As far as I can see, the PEP under discussion (which simply replaces the
word "range" by "iter", creates an iterator instead of a list, and perhaps
only allows the single-argument version of range) doesn't help me at all in
my desire to have lecture notes which are both intuitively understandable
and runnable.
--
David Eppstein UC Irvine Dept. of Information & Computer Science
eppstein at ics.uci.edu http://www.ics.uci.edu/~eppstein/
More information about the Python-list
mailing list