New assignmens ...

Antoon Pardon antoon.pardon at vub.be
Wed Oct 27 05:09:35 EDT 2021


Op 27/10/2021 om 10:38 schreef dn via Python-list:
> On 24/10/2021 22.23, O365 Dict wrote:
>> Well I have the following use case:
>>
>>      while (temp_result := calculate_next_couple(a, b))[1]:
>>          a, b = temp_result
>>          more calculations
>>
>> Which IMO would be clearer if I could just write:
>>
>>      while ((a, b) := calculate_next_couple(a,b))[1]:
>>          more calculations
>>
>> Of course it would even more clear if I could write something like:
>>
>>      while (a, b) := calculate_next_couple(a, b); b:
>>          more calculations
>>
>> or
>>
>>      do:
>>          a, b = calculate_next_couple(a, b)
>>      while b:
>>          more calculations
>
> Found (all of) the above less-than-obvious to read. Putting it in front
> of trainees this morning caused only confusion - even the
> currently-legal variation.

A lot of python idioms are less than obvious for trainees when they first
encounter them. I don't think "Is obvious for trainess" is a good measure
to evaluate possible future programming constructs.

> Accordingly: is this a job for the walrus operator at all? Let's "talk
> of many [other] things"*.
>
> ...
>
>
> That all looks simple. What is dn complaining about?
>
>
> Could we use a data structure to continue to keep things straight-forward?

Yes off course we could. Before the walrus operator was introduced, we could
also introduce extra code so that we wouldn't need the walrus operator at all.


> ...
> Hope the above gives you some ideas/pause for thought!

No they don't. You have taken an example to illustrate an idea and
treated it as if it was some production code you had to review.

What you are suggesting boils downto that if one has a loop in which
one has two work variables, with a condition on those variable to
control a loop. In order to make use of the walrus operator we should
combine those two work variables into some kind of instance no matter
how contrived.

-- 
Antoon Pardon.




More information about the Python-list mailing list