[Python-Dev] (name := expression) doesn't fit the narrative of PEP 20
On Friday, April 27, 2018, Chris Angelico <rosuav at gmail.com> wrote:
> On Fri, Apr 27, 2018 at 8:18 PM, Wes Turner <wes.turner at gmail.com> wrote:
> > IDK, I could just be resistant to change, but this seems like something
> > will decrease readability -- and slow down code review -- without any
> > performance gain. So, while this would be useful for golfed-down (!)
> > one-liners with pyline,
> > I'm -1 on PEP 572.
> PEP 572 has never promised a performance gain, so "without any real
> performance gain" is hardly a criticism.
> > How do I step through this simple example with a debugger?
> > if re.search(pat, text) as match:
> > print("Found:", match.group(0))
> Step the 'if' statement. It will call re.search() and stash the result
> in 'match'. Then the cursor would be put either on the 'print' (if the
> RE matched) or on the next executable line (if it didn't).
Right. Pdb doesn't step through the AST branches of a line, so ternary
expressions and list comprehensions and defining variables at the end of
the line are 'debugged' after they're done.
Similarly, code coverage is line-based; so those expressions may appear to
be covered but are not.
> > From https://en.wikipedia.org/wiki/Assignment_(computer_science) :
> >> In some languages the symbol used is regarded as an operator (meaning
> >> the assignment has a value) while others define the assignment as a
> >> statement (meaning that it cannot be used in an expression).
> > PEP 572 -- Assignment Expressions
> > PEP 572 -- Assignment Operator (:=) and Assignment Expressions
> Huh? I don't get your point.
Q: What is ':='? (How do I searchengine for it?)
A: That's the assignment operator which only works in Python 3.8+.
Q: When are variables defined -- or mutable names bound -- at the end of
the expression accessible to the left of where they're defined?
Q: What about tuple unpacking? Is there an ECMAscript-like destructuring
A: Ternary expressions; list, dict, generator comprehensions;
(@DOCS PLEASE HELP EXPLAIN THIS)
Q: do these examples of the assignment expression operator all work?
- debuggers have no idea what to do with all of this on one line
- left-to-right doesn't apply to comprehensions
results = [(x, y, x/y) for x in input_data if (y := f(x)) > 0]
- left-to-right doesn't apply to ternary expressions
if (y := func(x)) if (x := 3) else 0:
while (y := func(x)) if (x := 3) else 0:
- left-to-right does apply to everything else?
- *these* are discouraged:
if (x := 3) or (y := func(x)):
if (3) or (func(3)):
if ((x := 3) if 1 else (y := func(x))):
> Python-Dev mailing list
> Python-Dev at python.org
> Unsubscribe: https://mail.python.org/mailman/options/python-dev/
-------------- next part --------------
An HTML attachment was scrubbed...