[Python-ideas] A real life example of "given"

Brendan Barnwell brenbarn at brenbarn.net
Thu May 31 14:03:56 EDT 2018

On 2018-05-31 05:53, Steven D'Aprano wrote:
> Bottom line is, if you think it is okay that the following assignment to
> x affects the local scope:
>      results = []
>      for a in seq:
>          # using "given" to avoid arguments about :=
>          y = (x given x = a)+1
>          results.append(y)
>      assert "x" in locals()
> but then worry that changing the loop to a comprehension:
>      results = [(x given x = a)+1 for a in seq]
>      assert "x" in locals()
> will be a problem, then I think you are applying an unreasonably strict
> standard of functional purity towards comprehensions, one which is not
> justified by Python's consenting adults approach to side-effects or the
> fact that comprehensions can already have side-effects.

	What I don't understand is this: if we believe that, then why was 
comprehension-leaking EVER removed?  Everything that I've seen 
advocating for this kind of leaking seems to me like it is much more 
logically consistent with allowing all comprehension variables to leak 
than it is with the current behavior, in which they don't leak.

Brendan Barnwell
"Do not follow where the path may lead.  Go, instead, where there is no 
path, and leave a trail."
    --author unknown

More information about the Python-ideas mailing list