[Python-ideas] Expose `itertools.count.start` and implement `itertools.count.__eq__` based on it, like `range`.

Terry Reedy tjreedy at udel.edu
Sat Jun 7 21:07:32 CEST 2014


On 6/6/2014 9:59 PM, Neil Girdhar wrote:
> That would be great.

What does 'that' refer too? Ram's original proposal, which you quoted, 
or Antony's counter-proposal, which you also quoted? Ambiguity is the 
cost of top-posting combined with over-quoting.

Since I already explained what is wrong with Ram's proposal, I will 
delete it and assume you mean Antony's, which seems to not have arrived 
on my machine.

> On Friday, May 16, 2014 12:16:52 AM UTC-4, Antony Lee wrote:
>
>     Actually, a more reasonable solution would be to have range handle
>     keyword arguments and map "range(start=x)"  to "count(x)".

Having a parameter like 'start' mean two different things when passed by 
position and name is the sort of oddity we try to avoid.

Since "a range object is an immutable, constant attribute, reiterable 
sequence object" (my earlier post), while count is an iterator, that 
does not literally work. So I will assume that you mean (looking ahead) 
'an iterable sis_range such that iter(sis_range(n, None, step)) is the 
same as count(n, step)'.

> Or,  perhaps more simply, "range(x, None)"

As an expression, stop=None is literally what you mean.

The problem is that range is a finite sequence with a finite length and 
an indexable end. For instance, range(10)[-1] == 9. What is needed is a 
new semi_infinite_sequence base class 'SemiInfSeq' that allows (but not 
requires) infinite length: float('inf') or a new int('inf'). It would 
also have to disallow negative ints for indexing and slicing. Or perhaps 
a class factory is needed.

Many infinite iterators whose items can be calculated from index n could 
be the iterator for a SIS subclass.  A geometric series and the sequence 
of squares are other examples. This could be a PyPI package.

-- 
Terry Jan Reedy



More information about the Python-ideas mailing list