On Sun, May 13, 2018 at 5:17 PM, Ethan Furman <et...@stoneleaf.us> wrote:
> On 05/12/2018 11:41 PM, Tim Peters wrote:
>>
>> [Tim, suggests changes to the Reference Manual's 4.2.1]
>>>
>>> """
>>> An assignment expression binds the target, except in a function F
>>> synthesized to implement a list comprehension or generator expression
>>> (see XXX).  In the latter case, if the target is not in F's
>>> environment (see section 4.2.2) , the target is bound in the block
>>> containing F.
>>> """
>>
>>
>> Let me try that again ;-)  The notion of "environment" includes the
>> global scope, but that's not really wanted here.   "Environment" has
>> more of a runtime flavor anyway.  And since nobody will tell me
>> anything about class scope, I read the docs myself ;-)
>>
>> And that's a problem, I think!  If a comprehension C is in class scope
>> S, apparently the class locals are _not_ in C's environment.  Since C
>> doesn't even have read access to S's locals, it seems to me bizarre
>> that ":=" could _create_ a local in S.
>
>
> Python 3.7.0b3+ (heads/bpo-33217-dirty:28c1790, Apr  5 2018, 13:10:10)
> [GCC 4.8.2] on linux
> Type "help", "copyright", "credits" or "license" for more information.
> --> class C:
> ...   huh = 7
> ...   hah = [i for i in range(huh)]
> ...
> --> C.hah
> [0, 1, 2, 3, 4, 5, 6]
>
> Same results clear back to 3.3 (the oldest version of 3 I have).  Are the
> docs wrong?

>>> class C:
...     huh = 7
...     hah = [i for _ in [0] for i in range(huh)]
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in C
  File "<stdin>", line 3, in <listcomp>
NameError: name 'huh' is not defined

The outermost iterable is a special case. Otherwise, you can't
reference anything.

ChrisA
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to