[Python-ideas] Unbounded range(), was Re: Allow __len__ to return infinity

Peter Otten __peter__ at web.de
Tue Feb 25 13:38:18 CET 2014


Ram Rachum wrote:

> This actually makes me think that `range(int('inf'))` is a more elegant
> construct than `itertools.count()`. Also `range(x, int('inf'))`
> for `itertools.count(x)`, 

You could achieve that with range(None) or range(start, None)
which would be similar to slices like items[start:None] aka items[start:].

> and then you have `range(x, int('inf'), y)` or `range(0, int('-inf'), -1)` 
> which `itertools.count` can't cover.

This *is* covered by count():

>>> [x for x in itertools.islice(itertools.count(step=-2), 10)]
[0, -2, -4, -6, -8, -10, -12, -14, -16, -18]

All you save is one import. In return you'll see your applications break in 
new and interesting ways ;)



More information about the Python-ideas mailing list