[Python-ideas] More useful slices
Greg Ewing
greg.ewing at canterbury.ac.nz
Mon Feb 2 23:25:54 CET 2015
Skip Montanaro wrote:
> optimizers like PyPy which aim to be precisely compatible with CPython
> semantics must look up "range" every time it occurs and decide if it's
> the real builtin range function, in which case it can emit/execute
> machine code which is something like the C for loop:
>
> for (i=start; i<stop; i+=step) {
> do something interesting with i
> }
I'd rather have a more flexible way of ierating over integer
ranges that's not biased towards closed-start open-end
intervals.
While that's convenient for indexing sequences,
if that's all you want to do you're better off iterating
over the sequence directly, maybe using enumerate and/or
zip. If you really need to iterate over ints, you
probably need them for some other purpose, in which
case you're likely to want any combination of
open/closed start/end.
For Pyrex I came up with a syntax that allows specifying
any combination equally easily and clearly:
for 0 <= i < 10:
# closed start, open end
for 0 <= i <= 10:
# both ends closed
for 0 < i < 10:
# both ends open
for 10 >= i >= 0:
# both ends closed, going backwards
etc.
I think something like this would be a much better use
of new syntax than just syntactic sugar for something
that's not used very often.
--
Greg
More information about the Python-ideas
mailing list