[Chris Angelico <ros...@gmail.com>]
> ...
> Not qualitative, but anecdotal: I do sometimes have to remind my
> JavaScript students to check whether they've typed enough equals
> signs. And that's in a language where the normal comparison operator
> is ===. It's *still* not uncommon to see a comparison spelled =.

I wonder whether Guido remembers this ;-)  In the very, very, VERY
early days, Python didn't have "==".  Plain single "=" was used for
both assignment and equality testing.  So way back then. using "=" for
embedded assignment too was intractable on the face of it.

I'm not clear on why it changed.  I remember writing to Guido about
how to disambiguate between the "bind" and "test for equality" intents
in isolated expressions typed at the interactive prompt, and next
thing I knew the language changed to use "==" for the latter.

In any case, I really don't want to see plain "=" for embedded
assignments now.  It's been the source of some of the worst C
debugging nightmares I've wasted months of my life tracking down.
Here's one that baffled an office full of MIT grads for half a day
before I noticed the real problem:

    assert(n=2);

You can fill in the rest of the story yourself - but you'll miss the
full extent of the agony it caused ;-)

Guido's original intuition was right:  regardless of programming
experience, it remains sorely tempting to write "x = y" when equality
testing is intended.  To this day I routinely get a syntax error in
Python when doing that by mistake.  For which I'm eternally grateful.

Any other way of spelling it would be preferable.  Requiring
parentheses around it isn't enough; e.g.,

    if (x = 1) or (y = 2):

would almost certainly not do what was intended either.  There's also
that many newcomers from C-like languages habitually put all `if` and
`while` tests in parens.

I'm fond enough of ":=".  Icon used that for assignment (embedded or
otherwise), and I don't recall any bugs due to that.  It was hard to
confuse for "==" (whether conceptual confusion or visual confusion).

That was just prone to the _other_ problem with embedded assignments:
staring and staring trying to find the code where a name was most
recently bound - "oh!  it was bound inside the third nested clause in
the `while` test two pages back".  So it would be nice to combine
embedded assignment with some notion of limited scope - but I'm much
more concerned that the spelling not be easily confusable with "==".
But not really a fan of overly wordy spellings either.

There you go!  All the rest follows trivially from the Zen of Python ;-)
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to