On 28.06.2018 2:44, Greg Ewing wrote:
Ivan Pozdeev via Python-Dev wrote:
for me, the primary use case for an assignment expression is to be able to "catch" a value into a variable in places where I can't put an assignment statement in, like the infamous `if re.match() is not None'.

This seems to be one of only about two uses for assignment
expressions that gets regularly brought up. The other is
the loop-and-a-half, which is already adequately addressed
by iterators.

So maybe instead of introducing an out-of-control sledgehammer
in the form of ":=", we could think about addressing this
particular case.

Like maybe adding an "as" clause to if-statements:

   if pattern.match(s) as m:
      do_something_with(m)


I've skimmed for the origins of "as" (which I remember seeing maybe even before Py3 was a thing) and found this excellent analysis of modern languages which is too a part of the PEP 572 discussion:
https://mail.python.org/pipermail/python-ideas/2018-May/050920.html

It basically concludes that most recently-created languages do not have assignment expressions; they rather allow assignment statement(s?) before the tested expression in block statements (only if/while is mentioned. `for' is not applicable because its exit condition in Python is always the iterable's exhaustion, there's nothing in it that could be used as a variable).

It, however, doesn't say anything about constructs that are not block statements but are equivalent to them, like the ternary operator. (In comprehensions, filter conditions are the bits equivalent to if/while statements.)

--
Regards,
Ivan

_______________________________________________
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

Reply via email to