[Tutor] Beginners question

Peter Otten __peter__ at web.de
Fri Apr 3 05:19:51 EDT 2020


DL Neil via Tutor wrote:

> With apologies to the OP for 'hi-jacking' the thread, a further
> question, if I may:-
> 
> 
> On 1/04/20 12:50 AM, Peter Otten wrote:
> 
>>>>> while True:
>>>>>   # get input
>>>>>   # if input means quit
>>>>>   break   # break out of the loop
>>>>>   # proceed with whatever you want to do
> 
>>>> The above always looks so untidy to my eye. Could we use the 'walrus
>>>> operator' instead of while True:?

>> While I prefer the old-fashioned way here's how to spell the while loop
>> without break:
> 
>>>>> while (word:=input("Enter a word: ")) != "quit":
>> ...     print(f"The word is {word!r}")
 
> @Peter: why the preference for "the old-fashioned way"?
> (perhaps answered below...)

Indeed ;) 

Basically an expression with embedded assignments appears as "untidy" as a 
loop with breaks. I read PEP 572, and the only convincing examples are those 
from copy.py and sysconfig.py where a sequence of tests that required nested 
if-s and can be flattened into if...elif...elif...

Over the years there were some really cool additions to Python (generators, 
list comprehensions, context managers), but for some time my impression is 
that the language is going from lean and mean to baroque.




More information about the Tutor mailing list