[Tutor] Difference between 'yield' and 'print'
Kent Johnson
kent37 at tds.net
Wed Jan 17 12:00:24 CET 2007
Luke Paireepinart wrote:
> Refer to http://docs.python.org/ref/yield.html for information about yield.
> Disclaimer: The following information I'm going to present you I came up
> with after reading that short explanation of 'yield' so it may not be
> exactly correct.
There is a longer explanation here:
http://www.python.org/doc/2.2.3/whatsnew/node5.html
> So you could think of the loop like this:
> gen = some_function(5) #create the generator
> while 1:
> try:
> print gen.next()
> except: #we ran out of things to generate (the generator stopped
> yielding results -- supposedly it raises an error here?)
> print "End of generator."
> break
>
> Anyway, I've never used them but that sounds like a reasonable way for
> them to work.
Yes, that is pretty much what is going on. The generatory will raise
StopIteration when there are no more values.
Kent
More information about the Tutor
mailing list