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

Steven D'Aprano steve at pearwood.info
Fri May 11 11:10:45 EDT 2018


On Thu, May 10, 2018 at 10:31:00PM -0400, Nick Malaguti wrote:

> One of my  hurdles for ":=" is understanding when I should use it rather
> than "=". Should I use it everywhere? Should I use it only where I can't
> use regular "="? Is it a personal choice? Will it become so common that
> I need to think harder because some people will use it really frequently
> or intermix them?

When should I use "x = x + 1" and when should I use "x += 1"? When 
should I write alist.sort() and when sorted(alist)? When should I use a 
list comp and when should I use a for-loop? I could give a dozen more 
examples.

We always have a choice in writing code. There is almost never *only* 
one way to do it. Nevertheless, we manage, and I believe that most of 
the things which perplex us today will be so trivially easy to 
understand tomorrow that we'll wonder what the fuss was all about.

It is sometimes humbling to remember back at the things that we swore 
were terrible, terrible mistakes. I hated augmented assignment, until I 
gave in and started using them. I hated != instead of <> and I actually 
seriously considered using "from __future__ import barry_as_FLUFL" for a 
while.

Depending on whether you are a "glass half full" or "glass half empty" 
kind of guy, you could say either that:

- we're irrationally risk adverse, and would rather miss out on 
  something fantastic than risk something slightly not so good;

- or like Stockholm Syndrome victims, we can get used to, and even 
  learn to enjoy, the most awful crap if it goes on long enough.

(Or possibly both at the same time.)

Personally, I think that in hindsight my dislike of != was irrational 
and rather silly, rather than a prophetic realisation that the dropping 
of <> began the ruination of Python. YMMV.


> I don't want to see "=" vs ":=" become like semicolons in JavaScript.

The difference between = and := is nothing like automatic semicolon 
insertion in Javascript. The Python interpreter is never going to (only 
sometimes) insert a colon to make your "name = expression" work and it 
is never going to turn two statements into a single broken expression 
because you used = instead of := or vice versa.

Of course people can still screw up the precedence and get the wrong 
results, but the same applies to all operators and the same solution 
applies: when in doubt, add parentheses to make it clear.

Some arguments against := are better than others: will it encourage 
people to write unreadable one-liners instead of using multiple 
statements? Maybe, but I think most people will show more sense and 
restraint. You haven't seen many quadruply-nested list comprehensions, 
or ternary if operators nested ten deep, have you? I haven't.


> When I work on a different codebase, am I going to have to follow an
> "always" or "never" for binding expressions?

Yes, of course you are. Some places will be conservative and say Never, 
and some will be gung ho and say Always, but the majority will say "Use 
them when they make the code better".

Just like when you work on some code bases you will have to always 
follow PEP 8, and when you work on other code bases, you will have to 
follow different rules.


> Maybe this is all overblown
> and PEP8 direction will keep everyone on the same page, but I guess I
> worry about there being 2 very similar, but not the same, ways to do it.
> What I really like about "given" is it makes it a lot clearer when I
> should use it.

You seem to have two contradictory opinions here. Paraphrasing:

    "I worry that the same people who never abuse ternary if by
    writing obfuscated one-liners will suddenly turn around and
    abuse := binding expressions by writing obfuscated one-liners."

and

    "clearly nobody will abuse 'given' binding expressions by
    writing obfuscated one-liners, because they don't abuse 
    ternary if to write obfuscated one-liners."

I understand being pessimistic about the common-sense of my fellow 
programmers. I understand being optimistic about the common-sense of my 
fellow programmers. I don't understand doing both at the same time.

If people will abuse := they will abuse "given". If they won't abuse 
"given", they surely won't abuse := either. Since we have over a quarter 
of a century of experience showing that the Python community, as a 
whole, tends not to abuse syntax to write unreadable one-liners, I think 
that fears about people abusing := are overblown.



-- 
Steve


More information about the Python-ideas mailing list