[Python-Dev] Assignment expression and coding style: the while True case (original) (raw)

Rob Cliffe rob.cliffe at btinternet.com
Wed Jul 4 21:25:07 EDT 2018


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 -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20180705/e42986fe/attachment.html>



More information about the Python-Dev mailing list