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

Ed Kellett e+python-ideas at kellett.im
Fri May 11 17:56:58 EDT 2018

On 2018-05-11 22:12, Angus Hollands wrote:
> while (value:=get_next_pool_item()) and value.in_use:
>     print(value.refcount())

Just as a heads-up, I believe the prescribed way of doing that is:

    while (value := get_next_pool_item()).in_use:

Of course you'd need additional mess to do something else with value. I
don't like the asymmetry here:

    while (value := get_next_pool_item()).in_use and value is not blah:

> Secondly, it reads in the order that matters. When reading the first line,
> one encounters what the condition is evaluating *first*, and then the
> implementation details (m=p.match) second. It reads as one would describe a
> mathematical equation in a paper, and clearly separates *what you're
> interested in* from *what it depends upon*. This is what I particularly
> dislike about the ":=" operator approach, the value, and name it is bound
> to, are unrelated at the point of evaluation, yet are right next to each
> other. It's visual noise. In the example above, the reader has to read the
> entire line when trying to find the loop condition.

I'm inclined to agree. But several people have argued that this is more
readable than the alternative. I don't buy the reasoning, but they still
like it better, and there's probably no point in going any further into
this aspect. I doubt people are going to be convinced.
> What am I getting at here? In effect, the "given" keyword provides a
> superset of use cases to that of ":=". Dare I say it, but *explicit is
> better than implicit*.

I'm not sure that it's strictly a superset. It's arguably the reverse,
since it's restricted to statements with a condition rather than
arbitrary expressions. I think the more important thing is that
it's--subjectively--better at the subset of use cases that people seem
to actually have (as listed in the OP).

> *Readability:*
> A smaller point is that I don't feel that ":=" is very readable. If we had
> to use an operator, I think $= is better, but me reasoning for this is
> weak. I think it derives from my observation that ":=" is slow to
> distinguish from "=".

Clearly the objectively best choice is "<-".

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 488 bytes
Desc: OpenPGP digital signature
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180511/7b8ef4a9/attachment.sig>

More information about the Python-ideas mailing list