This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Make itertools count, cycle, and repeat objects subscriptable like range.
Type: enhancement Stage: needs patch
Components: Library (Lib) Versions: Python 3.4
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: NeilGirdhar, eric.araujo, jcea, rhettinger, serhiy.storchaka
Priority: normal Keywords:

Created on 2012-11-23 16:51 by NeilGirdhar, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (10)
msg176183 - (view) Author: Neil Girdhar (NeilGirdhar) * Date: 2012-11-23 16:51
When using sequence types polymorphically, range, list, and tuple are both iterable and subscriptable.  Unfortunately, itertools.count, cycle, and repeat objects are not subscriptable, although this is not a hard change.

Please consider making these objects subscriptable for polymorphic usage.
msg176184 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2012-11-23 17:01
Range, list, and tuple are iterables, but itertools.count, cycle, and repeat objects are iterators. Subscripting is not a part of iterator protocol.
msg176186 - (view) Author: Neil Girdhar (NeilGirdhar) * Date: 2012-11-23 17:11
Apologies if this is a bad question, but why do count, cycle, and repeat return iterators rather than iterables?
msg176190 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2012-11-23 17:20
The point of itertools is to implement building blocks for iterators, which are memory-efficient and can sometimes be infinite, contrary to sequences.
msg176196 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2012-11-23 17:45
> Apologies if this is a bad question, but why do count, cycle, and repeat return iterators rather than iterables?

Actually they are iterables too.
msg176221 - (view) Author: Neil Girdhar (NeilGirdhar) * Date: 2012-11-23 19:35
My suggestion is then to update collection.abc to have an InfiniteSequence, which inherits from Iterable, and adds abstract methods __getitem__ and mixin methods __iter__.

Then, itertools count, cycle, and repeat could implement collection.abc.InfiniteSequence, and collections.abc.Iterator (for backwards-compatibility).
msg176222 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2012-11-23 19:39
What kind of problem you want to solve? I'm sure there is a simple enough solution without changing itertools objects.
msg176225 - (view) Author: Neil Girdhar (NeilGirdhar) * Date: 2012-11-23 19:45
My code looks like this:

presignal_abd = [[], [0.1, 0.6], []]
tarsignal_abd = [[], [0.4, 0.9], []]
diagsignal_abd = [[], [0.1, 0.6, 0.7, 0.8], []]
# etc.

for (filename,
     observations,
     presignals,
     tarsignals,
     diagsignals,
     diagram_type) in zip(['events-deduction', 'events-abduction', 'events-accomodation', 'events-learning', 'events-learning-2'],
                          [observations_ded, observations_abd, observations_tra, observations_tra1, observations_tra],
                          [repeat_([]), presignal_abd, presignal_tra, repeat_([]), presignal_tra],
                          [repeat_([]), tarsignal_abd, tarsignal_tra, repeat_([]), tarsignal_tra],
                          [repeat_([]), diagsignal_abd, diagsignal_tra, repeat_([]), diagsignal_tra],
                          [0, 1, 2, 3, 3]):
    second_set_of_events = presignals[1]


I am using repeat_ objects (itertools.repeat objects) to fill as dummies.
msg176228 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2012-11-23 19:56
Why not use `dummy = [[]] * 3` as dummy?
msg176246 - (view) Author: Neil Girdhar (NeilGirdhar) * Date: 2012-11-23 20:29
Thanks, that works.

One of the things I like about Python is that you can write what you mean.  I figured that since I meant "repeat [] as many times as necessary", that I should write it that way.  So, from an intuitive standpoint, I still feel that these itertools types intuitively implement InfiniteSequence.  Although I recognize that some people might see them only in the narrower sense of Iterators.
History
Date User Action Args
2022-04-11 14:57:38adminsetgithub: 60744
2012-11-26 17:15:55jceasetnosy: + jcea
2012-11-24 00:02:46rhettingersetstatus: open -> closed
resolution: rejected
2012-11-23 20:29:53NeilGirdharsetmessages: + msg176246
2012-11-23 19:56:28serhiy.storchakasetmessages: + msg176228
2012-11-23 19:45:01NeilGirdharsetmessages: + msg176225
2012-11-23 19:39:46serhiy.storchakasetmessages: + msg176222
2012-11-23 19:35:32NeilGirdharsetmessages: + msg176221
2012-11-23 17:45:49serhiy.storchakasetmessages: + msg176196
2012-11-23 17:20:01eric.araujosetnosy: + eric.araujo
messages: + msg176190
2012-11-23 17:11:22NeilGirdharsetmessages: + msg176186
2012-11-23 17:01:23serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg176184
2012-11-23 16:55:51ezio.melottisetnosy: + rhettinger
stage: needs patch

versions: - Python 3.1, Python 3.2, Python 3.3, Python 3.5
2012-11-23 16:51:40NeilGirdharcreate