Author bruno.loff
Recipients bruno.loff, congma, docs@python, terry.reedy
Date 2021-03-27.10:25:29
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <>
Hmm yes, some more words in the documentation might help.

Does anyone understand why it happens, though?

Specifically, note that

sum(get(i) for i in range(len(l)))


eval("get(0) + get(1) + get(2) + get(3)")


eval("sum(get(i) for i in range(len(l)))", locals())

work just fine, but

eval("sum(get(i) for i in range(len(l)))")

fails, which is really confusing. I have no mental model of what is happening that allows for the first thing to work, but disallows for the second thing. I understand it has something to do with the creation of a new subscope when the comprehension is run, but other than that, I don't really understand.

Also, ideally, the last thing should work, too.

I am teaching programming to college students and I could not explain to them why the first three worked but the last one failed. There was simply nothing I could say that would give them a good mental model for the execution.
Date User Action Args
2021-03-27 10:25:29bruno.loffsetrecipients: + bruno.loff, terry.reedy, docs@python, congma
2021-03-27 10:25:29bruno.loffsetmessageid: <>
2021-03-27 10:25:29bruno.lofflinkissue43605 messages
2021-03-27 10:25:29bruno.loffcreate