Thoughts on PEP284

Daniel Dittmar daniel.dittmar at sap.com
Tue Sep 23 05:32:59 EDT 2003


Stephen Horne wrote:
>   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).

Adding operators to types is always problematic because it defeats Pythons's
runtime checking. Assuming that integer slicing would be added to Python,
methods that would expect a list of integers would suddenly also work with
integers. In most cases, they would not work correctly, but you wouldn't get
a meaningful exception.

> 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
>     ...

I'm sure that you can think of a generator function that does exactly the
same.

Daniel







More information about the Python-list mailing list