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

Chris Angelico rosuav at gmail.com
Fri May 11 09:02:43 EDT 2018

On Fri, May 11, 2018 at 9:37 PM, Rhodri James <rhodri at kynesim.co.uk> wrote:
> On 11/05/18 11:14, Jacco van Dorp wrote:
>> 2018-05-11 11:56 GMT+02:00 João Santos <jmcs at jsantos.eu>:
>>> Optimizing syntax for space makes sense for "mathematical" notation since
>>> it's commonly written by hand, but putting space above readability in a
>>> programming language design feels like a skewmorphism.
>> You are assuming "given" to improve readability, where I stated ":= is
>> perfectly clear ", at least in my opinion. Therefore, since clarity is
>> already achieved, the rest is clutter that reduces readability.
> 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()

Yes, I've considered it. And I don't like the fact that the evaluation
is right-to-left. It isn't a problem when your condition is extremely
simple, but I can guarantee you that people will use this with more
complicated conditions. And when that happens, you have to be aware
that the tail of the statement is actually evaluated before the
primary expression. It's like with Perl:

die("blah blah") unless some_condition

Reverse-order evaluation is confusing and frequently annoying. It's
not an instant failure of the proposal, but it's a serious cost, and
I'd much rather avoid it by using := (which leaves the expression
where it is).


More information about the Python-ideas mailing list