[Python-ideas] Fixing class scope brainstorm

Brice Parent contact at brice.xyz
Thu Mar 29 06:19:35 EDT 2018


> Hm, so maybe we shouldn't touch lambda, but we can at least fix the 
> scope issues for comprehensions and genexprs.
>
> There may still be breakage, when the code defines a global x that is 
> overridden by a class-level x, and a class-level comprehension 
> references x assuming it to be the global. So we need to tread 
> carefully even here -- but this case is weird already:
>
> x = 42
> class C:
>     x = [1, 2, 3]
>     z = [x+y for y in x]  # [43, 44, 45]
>
Wow!
I had to try it myself!

If I had came across something like the following in a code review :
x = [1, 2]
class C:
     x = [3, 4, 5]
     z = [x for _ in x]

I would have expected C.z to equal either `[[1, 2], [1, 2]]` or `[[3, 4, 
5], [3, 4, 5], [3, 4, 5]]`, but surely not `[[1, 2], [1, 2], [1, 2]]`!

Is that intentional, or the result of other way-more-logical decisions?

- Brice
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180329/ce27897d/attachment.html>


More information about the Python-ideas mailing list