[Python-ideas] Inline assignments using "given" clauses

Jacco van Dorp j.van.dorp at deonet.nl
Fri May 11 09:25:40 EDT 2018


>[Rhodri]
> I respectfully disagree with your opinion (i.e. you're wrong :-)
>
> Consider:
>
>   while (cmd := get_command()).token != CMD_QUIT:
>     cmd.do_something()
>
> vs:
>
>   while cmd.token != CMD_QUIT given cmd = get_command():
>     cmd.do_something()
>

Actually, the first version is more readable. It's got a lot to do
with what Chris said about order of operations, but IMO, even more
with grouping. There's a couple of things you might want to learn from
this statement. First, will the while check succeed? Well....


while......get_command()).token != CMD_QUIT:

yup, looks clear. I can ignore the cmd := part easily, and the bonus
paren I see there doesn't matter that much.

Another thing i might be curious about, is what is the value of cmd after ?

while (cmd := get_command())........................:

Looks like it has the value of getcommand().

Hey, that was both clear and readable. I can just ignore half the line
and learn stuff. Great.

Lets see with the other notation. What's the value of cmd ?

 while ................................................... cmd = get_command():

That works. Bit of line I had to skip.

Will the check succeed ?

while cmd.token != CMD_QUIT .....................................:

Wait, what's the value of cmd ? Lets look in the code in the preceding
lines....oh, ok, it's at the end of the line.

I actually have to mentally parse the entire line to get what the
check will work. This, along with what Chris said about order of
operations, reduce the readability of the "given" version.


More information about the Python-ideas mailing list