New assignmens ...

Antoon Pardon antoon.pardon at vub.be
Wed Oct 27 06:45:25 EDT 2021


Op 27/10/2021 om 11:59 schreef Chris Angelico:
> You can argue the word "need" all you like, but the fact remains that
> YOU want a change, so YOU have to convince people of the benefits.

That is true. But there is nothing wrong in asking what might convince
people.

But I'll give you my thought below and you can decide in how far this
is convincing to you.

I regularly come with a problem for which a one and a half loop is very
suited to solve it. Unfortunately python doesn't have a one and a half
loop. However with the introduction of the walrus operator there is a
way to simulate a significant number of one and a half loops.

Consider the following:

     do
         a = expr1
         b = expr2
    while 2 * a > b:
        more calculations

We could write that now as

     while [
         a := expr1,
         b := expr2,
     2 * a > b][-1]:
         more calculations

Now there is some ugly side on the above and it may not be obvious at first
what is going on, but once you understand it is a rather easy idiom. I certainly
prefer it over writing something like

     while True:
         a = expr1
         b = expr2
         if not (2 * a > b):
             break
         more calculations.

So for me any limitation on the walrus operator that is removed is a plus because
it will allow me to write more one and a half loops in more natural way.

Off course should the python developers decide to intoduce a real one and a half
loop all the above is probably a whole let useful.

-- 
Antoon Pardon



More information about the Python-list mailing list