Author arigo
Recipients
Date 2004-02-03.11:46:14
SpamBayes Score
Marked as misclassified
Message-id
In-reply-to
Content
Logged In: YES 
user_id=4771

After thinking a bit more on the issue, note that the generator version of your example is equivalent to:

def g():
    for y in range(3):
        yield lambda x: x+y

which means that it can suffer from the same problem as the first example, but more subtly (and even more confusingly):

for f in g(): print f(0)         # 0, 1, 2
for f in list(g()): print f(0)   # 2, 2, 2

This is because due to Python's nested scope rules the above generator is equivalent to:

def g():
    result = lambda x: x+y
    for y in range(3):
        yield result

at which point I think it gets pretty confusing...
History
Date User Action Args
2007-08-23 15:31:37adminlinkissue872326 messages
2007-08-23 15:31:37admincreate