[Python-ideas] More useful slices
Rob Cliffe
rob.cliffe at btinternet.com
Mon Feb 2 23:44:39 CET 2015
On 02/02/2015 22:25, Greg Ewing wrote:
> 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.
>
I like it!
Much clearer than any other suggested syntax.
Flexible: Allows closed or open ends, whichever is more convenient or
better style.
Unambiguous: 'for' not followed by non-nested 'in' is currently not
valid syntax.
Efficient: allows faster bytecode to be generated.
+1
Rob Cliffe
More information about the Python-ideas
mailing list