Issue 33049: itertools.count() confusingly mentions zip() and sequence numbers (original) (raw)
From the itertools documentation: https://docs.python.org/3/library/itertools.html?highlight=itertools#itertools.count
Also, used with zip() to add sequence numbers.
I'm not certain what the goal of the original sentence was, but I think it's unclear as currently written.
I assume this is what's meant:
my_sequence = [1, 2, 3, 4] for i, item in zip(count(1), my_sequence): print(i, item)
This is a strange thing to note though because enumerate would be a better use here.
my_sequence = [1, 2, 3, 4] for i, item in enumerate(my_sequence, start=1): print(i, item)
Maybe what is meant is that count can be used with a step while enumerate cannot?
my_sequence = [1, 2, 3, 4] for i, item in zip(count(step=5), my_sequence): print(i, item)
If that's the case it seems like step should instead be mentioned there instead of "sequence numbers".
This is a strange thing to note though because enumerate would be a better use here.
IIRC, the wording predates the addition of enumerate() and before enumerate() grew a start argument. That said, enumerate() just addresses care the most common case. It is still worth mentioning that count() is still useful for the general case of adding sequence numbers to data streams woven together by zip() -- like the way auto-increment is used in SQL:
from time import ctime
def timestamp():
while True:
yield ctime()
def user_request():
while True:
yield input()
logged_requests = zip(user_request(), count(1), timestamp(), cycle(available_servers))
it seems like step should instead be mentioned there instead of "sequence numbers".
The step argument was a late addition to the API and isn't used much in practice. When it is used, its meaning and use case tend to be self-evident (i.e. counting 60 seconds at a time, or counting backwards), so it doesn't warrant further elaboration.
The sentence as-is is imperfect (it makes you wonder why not just use enumerate) but it seems better than either saying less by not mentioning the use case or getting too wordy which would place too much emphasis on use cases less common that those served by enumerate(). So, my preference is to leave the sentence as it stands. The intent of the two sentences mentioning map() and zip() was to hint at the possibilities while still keeping the paragraph primary focused on what count() actually does.