[Python-Dev] assignment expressions: an alternative proposal (original) (raw)

Yury Selivanov yselivanov.ml at gmail.com
Tue Apr 24 11:35:20 EDT 2018


On Tue, Apr 24, 2018 at 11:31 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:

On 25 April 2018 at 00:54, Eric Snow <ericsnowcurrently at gmail.com> wrote:

Regardless, your 3 rules would benefit either syntax. Nick may have a point that the rules might be an excessive burden, but I don't think it's too big a deal since the restrictions are few (and align with the most likely usage) and are limited to syntax so the compiler will be quick to point mistakes. I think the "single name target only" rule should be in place no matter the syntax for the name binding operator itself. I don't mind too much either way on the mandatory parentheses question (it's certainly an easy option to actively discourage use of binding expressions as a direct alternative to assignment statements, but as with the single-name-only rule, it's independent of the choice of syntax)

Mandatory parenthesis around (name := expr) would at least solve the problem of users mixing up '=' and ':=' in statements.

I do think the "no name rebinding except in a while loop header" restriction would be annoying for the if/elif use case and the while use case: while (item = getitem()) is not firstdelimiter: # First processing loop while (item = getitem()) is not seconddelimiter: # Second processing loop # etc... if (target = getfirstcandidate()) is not None: ... elif (target = getsecondcandidate()) is not None: ... elif (target = getthirdcandidate()) is not None: ...

Yes, it would force users to come up with better names iff they want to use this new sugar:

if (first_target = get_first_candidate()) ... elif (second_target = get_second_candidate()) ...

Yury



More information about the Python-Dev mailing list