Perhaps I’ve fallen victim to misinformation on the web, but I think it’s more likely just that I’ve misunderstood something. Based on what I’ve learned so far, range() is a generator, and generators can be used as iterators. However, this code:
myrange = range(10) print(next(myrange))
gives me this error:
TypeError: 'range' object is not an iterator
What am I missing here? I was expecting this to print 0, and to advance to the next value in myrange. I’m new to Python, so please accept my apologies for the rather basic question, but I couldn’t find a good explanation anywhere else.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
range is a class of immutable iterable objects. Their iteration behavior can be compared to lists: you can’t call next directly on them; you have to get an iterator by using iter.
So no, range is not a generator.
You may be thinking, “why didn’t they make it directly iterable”? Well, ranges have some useful properties that wouldn’t be possible that way:
- They are immutable, so they can be used as dictionary keys.
- They have the
start,stopandstepattributes (since Python 3.3),countandindexmethods and they supportin,lenand__getitem__operations. - You can iterate over the same
rangemultiple times.
>>> myrange = range(1, 21, 2) >>> myrange.start 1 >>> myrange.step 2 >>> myrange.index(17) 8 >>> myrange.index(18) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: 18 is not in range >>> it = iter(myrange) >>> it <range_iterator object at 0x7f504a9be960> >>> next(it) 1 >>> next(it) 3 >>> next(it) 5
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0