On 2020-11-18 03:36, Larry Hastings wrote:
[snip]
But then we get to generator expressions and list/dict/set
comprehensions. I think of those as doing an "assignment", but I have
to remember the assignment "doesn't leak":
x = 3
y = list(x**2 for x in range(5))
print(f"{x=}")
This code prints "x=3".
Why this exception to the rule? IIRC from the discussions back when
they were added, this one of those "it just seemed best at the time"
things. "for" was added to the language long before I got there, but it
"leaking" its assignment was judged useful; if you broke out of the
loop, you could continue to examine the last value from the loop. But,
the thinking went, you'd never want to examine the last value from a
list generator, so it was more convenient if it behaved as if it had its
own scope.
[snip]
I think the reason that generator expressions don't leak is that
experience of list comprehensions, which did leak, showed that it was a
bad idea. List comprehensions were "fixed" in Python 3.
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at
https://mail.python.org/archives/list/python-dev@python.org/message/Y7S6VWQOVWICPBDUE7MS7RHGA67AQULO/
Code of Conduct: http://python.org/psf/codeofconduct/