Odd name shadowing in comprehension

Chris Angelico rosuav at gmail.com
Sat Oct 22 21:28:08 EDT 2016


On Sun, Oct 23, 2016 at 11:51 AM, eryk sun <eryksun at gmail.com> wrote:
> It matches the behavior of generator expressions, for which Guido
> gives the following example, as quoted in PEP 289:
>
>     Consider sum(x for x in foo()). Now suppose there's a bug in foo()
>     that raises an exception, and a bug in sum() that raises an
>     exception before it starts iterating over its argument. Which
>     exception would you expect to see? I'd be surprised if the one in
>     sum() was raised rather the one in foo(), since the call to foo()
>     is part of the argument to sum(), and I expect arguments to be
>     processed before the function is called.

Fair enough, except that a generator expression is syntactic sugar for
a generator function, and the return value of a generator function is
a generator object that hasn't yet been started. So where the boundary
is, then, is a bit of a fuzzy line.

Thanks for digging that up, at least.

ChrisA



More information about the Python-list mailing list