(original) (raw)



On 05/07/2018 01:25, Tim Peters wrote:

== Pattern 5, two variables ==

while True:
m = match()
if not m:
break
j = m.end()
if i == j:
break
...

replaced with:

while (m := match()) and (j := m.end()) == i:
I assume (sorry to be pedantic :-)) this is a typo for
while (m := match()) and (j := m.end()) != i:
...

Maybe we reached here the maximum acceptable complexity of a single
Python line? :-)

It's at my limit. But, as in an earlier example, I'd be tempted to do "the obvious part":

while m:= match():
j = m.end()
if i == j::
break

Then the start reads like "while there's something \_to\_ look at::" and the body of the loop is happily guaranteed that there is.

.
Or you could compromise with this "intermediate density" version that does two "obvious parts":

while m:=match():
if (j:=m.end()) == i:
break

(or as I might write it

while m:=match():
if (j:=m.end()) == i: break

).
Some might prefer this as shorter than non-AE version but less dense than the one-liner. Others might not. De gustibus non est disputandum.
My conclusion: Assignment expressions are - like any other Python feature - a tool, to be used with discretion and judgement. Not the start of a competition to see who can write the most slick/unreadable code.
Regards
Rob Cliffe