=- and -= snag

Keith Thompson Keith.S.Thompson+u at gmail.com
Tue Mar 14 21:06:46 EDT 2023


"Morten W. Petersen" <morphex at gmail.com> writes:
> I was working in Python today, and sat there scratching my head as the
> numbers for calculations didn't add up.  It went into negative numbers,
> when that shouldn't have been possible.
>
> Turns out I had a very small typo, I had =- instead of -=.
>
> Isn't it unpythonic to be able to make a mistake like that?

Very early versions of C (around 1975 or so, before K&R1 was
published) actually used "op=" for compound assignment operators, so
`x =- 2` would subtract 2 from x.  It was changed to "=op" (`x -= 2`)
precisely to avoid this ambiguity that programmers kept running
into (people were less generous with whitespace back then).

As late as the late 1990s, I used a compiler (VAXC) that still
recognized the old-style compound assignment operators, though I
think it warned about them.

I thought "Pythonic" was more about how you write code than about the
design of the language.  But designing a language syntax so typos are
likely to be syntax errors rather than valid code with different
semantics is an interesting challenge.

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u at gmail.com
Working, but not speaking, for XCOM Labs
void Void(void) { Void(); } /* The recursive call of the void */


More information about the Python-list mailing list