On Wed, Apr 25, 2018 at 6:15 AM, Nick Coghlan <ncogh...@gmail.com> wrote:
> On 25 April 2018 at 13:56, Guido van Rossum <gu...@python.org> wrote: > > On Tue, Apr 24, 2018 at 8:24 PM, Nick Coghlan <ncogh...@gmail.com> > wrote: > >> > >> I also think it would be good for the PEP to spell out the following > >> semantic invariant for code running in a regular namespace: > >> > >> _rhs = expr > >> assert (target := _rhs) is _rhs and target is _rhs > >> > >> It's the restriction to single names as targets that makes it possible > >> to impose such a strong assertion about what the syntax means. > > > > Can you elaborate? I don't understand what you mean by this. > > The assertion is just spelling out in code form that given the name > binding expression "target := expr", then: > > 1. the result of the expression itself is "expr", exactly as if the > name binding was omitted > 2. that result is also bound to the name "target" in the current scope > > The preceding "_rhs = expr" line is included to make the invariant > generalise even to expressions that have side effects or can change > their output based on mutable system state. > > Ironically, that may be clearer if I use another assignment statement > to break it out as two separate invariants: > > _rhs = expr > _inline_result = (bound_name := _rhs) > assert _inline_result is _rhs > assert bound_name is _rhs > > By contrast, the protocols involved in handling more complex > assignment targets and in handling augmented assignment mean that it > wouldn't be possible to define a similarly simple invariant of what > they mean (since the exact runtime behaviour would be both type and > target dependent). > > While it's handy that one _could_ use any valid assignment target, allowing this wouldn't (IMHO) necessarily be a good idea. Binding/assignment expressions fit well into Python's semantics (where bindings copy references rather than data) precisely because names are effectively and straightforwardly shorthand for values at the point of assignment. Restricting the targets in target := expression to simple names would avoid a lot of the tricksiness that less experienced programmers might be tempter to indulge, leading to simpler code without undue restrictions on what can be done. The PEP is currently somewhat confused on naming, since it is entitled "Assignment Expressions" but appears to then exclusively use the name "named expressions" to reference the concept under "Syntax and Semantics" and "assignment expression" elsewhere. I'd prefer the term "name binding expressions," since that implies the stricture that more complex targets are excluded. Whatever is chosen, usage in the PEP should be consistent. regards Steve
_______________________________________________ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com