[Python-ideas] Generators are iterators
Oscar Benjamin
oscar.j.benjamin at gmail.com
Sat Dec 13 00:15:33 CET 2014
More information about the Python-ideas mailing list
Sat Dec 13 00:15:33 CET 2014
- Previous message: [Python-ideas] Generators are iterators
- Next message: [Python-ideas] Generators are iterators
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 12 December 2014 at 22:54, Terry Reedy <tjreedy at udel.edu> wrote: > On 12/12/2014 4:10 PM, Oscar Benjamin wrote: > >> The function was just supposed to generate the effect. More plausible >> examples can be made. I tripped over this myself once when doing >> something with iterators that required me to extract the first item >> before processing the rest. A simple demonstration: >> >> def sum(iterable): >> iterator = iter(iterable) >> total = next(iterator) >> for item in iterator: >> total += item >> return total > > > Examples like this has been posted on python-list, probably more than once. > The answer has been to catch StopIteration (which should only come from > next/__next__) and either return a default value or raise ValueError('cannot > sum an empty iterable'). I know what the solution is. For my purposes simply raising an exception (any exception that wasn't StopIteration) would have been sufficient. I'm just saying that it's a mistake that can easily be made. Since I made it I'm now super-suspicious of next() so I probably wouldn't make it again. This mistake is the root of all of the problems that PEP 479 attempts to solve. If you don't believe it is a problem that someone might make this mistake then it follows that PEP 479 is a massive mistake.
- Previous message: [Python-ideas] Generators are iterators
- Next message: [Python-ideas] Generators are iterators
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-ideas mailing list