Python: The Fake '-=-' Operator
While writing some Python, I accidentally swapped two characters:
a =- b # What I wrote
a -= b # What I intended to write
That’s “equals minus” instead of “minus equals”. I was initially suprised that the interpreter allowed it. Digging in, I found that a =- b
, is equivalent to a = -b
(spaces don’t matter), which is the same as a = -1 * b
. Rather than decrementing a
by the amount b
, I was assigning a
to be the inverse of b
.
This led my friend Ben and I to figure out that the following weird-looking statements are both syntactically correct and equivalent to one another.
a -=- b
a +=+ b
What they do and why they are equivalent are left as an exercise for the reader.