On what basis do you ascertain whether "==" would work correctly?
Please explain.

Hi Chris, I'm just glancing at the line of code, and doing a little thought experiment to see if it would get the same output if == was used instead. For a singleton like None or False or the class like "list" .. == will return the same answer as "is". Look at these lines;

    if mode is None:
    if type(items) is list:

If that code works with "is" it's going to work with == too. People are not used to seeing == in these cases, but it works:

>>> x = None
>>> x is None
True
>>> x == None
True
>>>
>>> t = type([1, 2, 3])
>>> t is list
True
>>> t == list
True
>>>
>>> fn = list.index
>>> fn is list.index
True
>>> fn == list.index
True

The situations where "is" is truly needed are rather esoteric.

Best,

Nick

On Mon, Aug 30, 2021 at 2:02 PM Chris Angelico <rosuav@gmail.com> wrote:
On Tue, Aug 31, 2021 at 6:52 AM Nick Parlante <nick@cs.stanford.edu> wrote:
>
> I claimed that uses of "is" where it is needed for correctness
> are quite rare. Let me back that up with a little data here.
>
> Just as a random supply of Python code, let's look at
> the first four Python modules where the name starts
> with a letter from the Python standard modules list
> https://docs.python.org/3/py-modindex.html :
> abc.py aifc.py argparse.py ast.py (The array module appears to be in C)
>
> Strip out PyDoc and string literals and just grep
> for " is " (code included below if you'd like to try
> it yourself). Look at those lines - how many of those
> uses of "is" are needed for correctness, where the "is"
> is really doing something, and how many would work
> fine with ==? The resulting lines of code are included below.
>
> There's about 90 uses of is/is-not in this sample.
> 100% of these uses would work correctly using ==.
> Not a single one of these uses actually relies on the "is"
> computation for correctness.

On what basis do you ascertain whether "==" would work correctly?
Please explain.

ChrisA
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-leave@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/5LM3UMQXPEXWGUPHPGMN2NQSDOJZ2LEZ/
Code of Conduct: http://python.org/psf/codeofconduct/