[Python-Dev] PEP 572: Assignment Expressions

Eric Fahlgren ericfahlgren at gmail.com
Tue Apr 17 23:24:33 EDT 2018


On Tue, Apr 17, 2018 at 6:20 PM, Steven D'Aprano <steve at pearwood.info>
wrote:

> If there are tests which intentionally verify this behaviour, that
> really hurts your position that the behaviour is an accident of
> implementation. It sounds like the behaviour is intended and required.
>

​It is nonetheless bizarre and unexpected behavior.

>>> prefix = 'global'
>>> [prefix+c for c in 'abc']
['globala', 'globalb', 'globalc']

>>> def func():
...     prefix = 'local'
...     print([prefix+c for c in 'abc'])
>>> func()
['locala', 'localb', 'localc']

>>> class klass:
...     prefix = 'classy'
...     items = [prefix+c for c in 'abc']
>>> print(klass.items)
['globala', 'globalb', 'globalc']​

In Python 2, that last one would produce 'classya' and friends, due to the
"broken" comprehension scope.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20180417/f510d192/attachment.html>


More information about the Python-Dev mailing list