I am analyzing the following code sample:
>>> def check(v):
... for t in v:
... yield t
... if t==2:
... yield "a"
Do I understand correctly that the two last lines of code will never be executed, because yield
works as return
. So, basically the first yield
in a for loop (before the if clause) will always be applied.
Is my understanding correct?
UPDATE:
I do not understand why do I get the sequence 2, a, 2, a, 1, ... and not this one: 1,a,1,...
>>> my_list = [2,2,1,2,3]
>>> f = check(my_list)
>>> f.next()
2
>>> f.next()
'a'
>>> f.next()
2
>>> f.next()
'a'
>>> f.next()
1
>>> f.next()
2