I wrote the following into the Python interpreter today:
>>> def test():
... for c in ['a', 'b', 'c', 'd']: yield c
...
>>> a = test()
>>> a
<generator object test at 0x2556a00>
>>> a.next()
'a'
>>> a.next()
'b'
This was surprising to me. Shouldn't test
return (or yield) one of the elements in my list, not a generator which yields them?
A second function which uses "return" behaves as expected:
>>> def test2():
... for i in ['a', 'b', 'c', 'd']: return i
...
>>> b = test2()
>>> b
'a'
>>> b
'a'
Why is this so? Where in the documentation describes this behaviour?