On Fri, Sep 3, 2021 at 3:24 PM Steven D'Aprano <st...@pearwood.info> wrote:
>
> On Thu, Sep 02, 2021 at 04:54:45PM +0900, Stephen J. Turnbull wrote:
> > Steven D'Aprano writes:
> >  > On Thu, Sep 02, 2021 at 04:04:40PM +0900, Stephen J. Turnbull wrote:
> >  >
> >  > > You may not need to teach them about singletons, though.
> >  >
> >  > It's hard to teach why `is` works with None,
> >
> > For definitions of "works" that comes down to "agrees with Nick that
> > 'is' is just a weird way to spell '==' most of the time".  But that's
> > not how I think of 'is'.
>
> Ah, but that's because you're a Python programmer who has been seeped in
> the language for many, many years :-)
>
> To most people, "is" and "equals" are synonyms, as in:
>
> * one and one is two;
> * two times four is eight;
> * the discriminant of a general quadratic a x^2 + bx + c is b^2-4ac
>
> etc. And there are programming languages where `is` *is* an equality
> test, e.g. Hypertalk lets you say things like:
>
>    if x + 1 is 2 then ...
>
>    if the first word of the last line of text is "Goodbye" then ...
>
>
> So for somebody who is expecting "is" to be a synonym for `==`, how do
> you explain what it actually is? You need to talk about the Python
> execution model, how all values are objects, and that the reason that
> `x is None` always does the right thing but `x is []` doesn't is that
> None is a singleton, so that there is only one object that is None, but
> the empty list is not, there are many, many distinct empty lists.
>

Yes, although I'd love to have a good term for "there is only ever one
object of this type AND VALUE", rather than "there is only one object
of this type", so that True and False could be discussed the same way
as singletons. String interning behaves the same way too, but I don't
want to say that None is an "inherently interned type" or anything
ridiculous like that!

Still, the two comparison operators "is" and "==" in Python are a lot
easier to explain than "==" and "===" in JavaScript. (Or, worse, PHP,
where two strings will be compared as numbers by ==.) In Python, it's
simple: "==" tests if they have equal value, and "is" tests if they're
the same object. And since it is that simple, you HAVE to have a
concept in your head of what it means to be "the same object", which
comes down to the object and reference model.

ChrisA
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/IZ3IND66VDTXKPSCYNNJOQQR26FBMZ64/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to