When I use a generator in a for loop, it seems to "know", when there are no more elements yielded. Now, I have to use a generator WITHOUT a for loop, and use next() by hand, to get the next element. My problem is, how do I know, if there are no more elements?
I know only: next() raises an exception (StopIteration), if there is nothing left, BUT isn't an exception a little bit too "heavy" for such a simple problem? Isn't there a method like has_next() or so?
The following lines should make clear, what I mean:
#!/usr/bin/python3
# define a list of some objects
bar = ['abc', 123, None, True, 456.789]
# our primitive generator
def foo(bar):
for b in bar:
yield b
# iterate, using the generator above
print('--- TEST A (for loop) ---')
for baz in foo(bar):
print(baz)
print()
# assign a new iterator to a variable
foobar = foo(bar)
print('--- TEST B (try-except) ---')
while True:
try:
print(foobar.__next__())
except StopIteration:
break
print()
# assign a new iterator to a variable
foobar = foo(bar)
# display generator members
print('--- GENERATOR MEMBERS ---')
print(', '.join(dir(foobar)))
The output is as follows:
--- TEST A (for loop) ---
abc
123
None
True
456.789
--- TEST B (try-except) ---
abc
123
None
True
456.789
--- GENERATOR MEMBERS ---
__class__, __delattr__, __doc__, __eq__, __format__, __ge__, __getattribute__, __gt__, __hash__, __init__, __iter__, __le__, __lt__, __name__, __ne__, __new__, __next__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__, close, gi_code, gi_frame, gi_running, send, throw
Thanks to everybody, and have a nice day! :)
This may not precisely answer your question, but I found my way here looking to elegantly grab a result from a generator without having to write a
try:
block. A little googling later I figured this out:Now
result
is either5
orNone
, depending on how many times you've called next on the iterator, or depending on whether the generator function returned early instead of yielding.I strongly prefer handling
None
as an output over raising for "normal" conditions, so dodging the try/catch here is a big win. If the situation calls for it, there's also an easy place to add a default other thanNone
.This is a great question. I'll try to show you how we can use Python's introspective abilities and open source to get an answer. We can use the
dis
module to peek behind the curtain and see how the CPython interpreter implements a for loop over an iterator.The juicy bit appears to be the FOR_ITER opcode. We can't dive any deeper using
dis
, so let's look up FOR_ITER in the CPython interpreter's source code. If you poke around, you'll find it inPython/ceval.c
; you can view it here. Here's the whole thing:Do you see how this works? We try to grab an item from the iterator; if we fail, we check what exception was raised. If it's
StopIteration
, we clear it and consider the iterator exhausted.So how does a for loop "just know" when an iterator has been exhausted? Answer: it doesn't -- it has to try and grab an element. But why?
Part of the answer is simplicity. Part of the beauty of implementing iterators is that you only have to define one operation: grab the next element. But more importantly, it makes iterators lazy: they'll only produce the values that they absolutely have to.
Finally, if you are really missing this feature, it's trivial to implement it yourself. Here's an example:
It is not possible to know beforehand about end-of-iterator in the general case, because arbitrary code may have to run to decide about the end. Buffering elements could help revealing things at costs - but this is rarely useful.
In practice the question arises when one wants to take only one or few elements from an iterator for now, but does not want to write that ugly exception handling code (as indicated in the question). Indeed it is non-pythonic to put the concept "
StopIteration
" into normal application code. And exception handling on python level is rather time-consuming - particularly when it's just about taking one element.The pythonic way to handle those situations best is either using
for .. break [.. else]
like:or using the builtin
next()
function with default likeor using iterator helpers e.g. from
itertools
module for rewiring things like:Some iterators however expose a "length hint" (PEP424) :
Note:
iterator.__next__()
should not be used by normal app code. That's why they renamed it fromiterator.next()
in Python2. And usingnext()
without default is not much better ...The two statements you wrote deal with finding the end of the generator in exactly the same way. The for-loop simply calls .next() until the StopIteration exception is raised and then it terminates.
http://docs.python.org/tutorial/classes.html#iterators
As such I don't think waiting for the StopIteration exception is a 'heavy' way to deal with the problem, it's the way that generators are designed to be used.