What is executed when in a generator

Jerzy Karczmarczuk karczma at info.unicaen.fr
Tue Oct 4 07:48:03 EDT 2005


I thought that the following sequence

gl=0
def gen(x):
     global gl
     gl=x
     yield x

s=gen(1)

suspends the generator just before the yield, so after the
assignment of s gl becomes 1.

Well, no. It is still zero. If I put

print "something"

before the yield, this doesn't get executed either. *EVERYTHING*
from the beginning until the yield gets executed only upon s.next().

Could you tell me please where can I read something in depth about the
semantics of generators? I feel a bit lost.
Thank you.


Jerzy Karczmarczuk



More information about the Python-list mailing list