[Python-Dev] PEP 572: Assignment Expressions
Nick Coghlan
ncoghlan at gmail.com
Sat Apr 21 02:17:42 EDT 2018
On 21 April 2018 at 07:33, Tim Peters <tim.peters at gmail.com> wrote:
> I expected that, given that expressions "naturally nest", chained
> targets could still be specified:
>
> a := b := c:= 5
>
> but since they're all plain names there's no way to tell whether the
> bindings occur "left to right" or "right to left" short of staring at
> the generated code.
The fact class namespaces are ordered by default now allow us to
demonstrate the order of multiple target assignments and tuple
unpacking without staring at generated code:
>>> class AssignmentOrder:
... a = b = c = 0
... d, e, f = range(3)
...
>>> class ReversedAssignmentOrder:
... c = b = a = 0
... f, e, d = range(3)
...
>>> [attr for attr in AssignmentOrder.__dict__ if not attr.startswith("_")]
['a', 'b', 'c', 'd', 'e', 'f']
>>> [attr for attr in ReversedAssignmentOrder.__dict__ if not attr.startswith("_")]
['c', 'b', 'a', 'f', 'e', 'd']
So that's a situation where "name = alias = value" could end up
matching "alias := name := value"
(Even in earlier versions, you can illustrate the same assignment
ordering behaviour with the enum module, and there it makes even more
of a difference, as it affects which name binding is considered the
canonical name, and which are considered aliases).
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
More information about the Python-Dev
mailing list