To me it seems like a useful shortcut that means:

 >>> x == True and type(x) == bool

But, as you say, it is best to just let the Python interpreter treat it as a boolean (i.e. within an `if`, it is converted automatically)

Now, I am thinking it would actually be *bad* to have the CPython implementation define `inf` singletons -- imagine a 3rd party library (for example, sympy) that defines '==' for new types:

```
>>> from sympy import oo
>>> oo == inf
True
>>> oo is inf
False
```
Again, it would be useful for a shortcut: `x is inf` is the same as `x == inf and type(x) == float`, but with 3rd party libraries I don't think it's something good to rely on.



On Mon, Sep 14, 2020, 12:05 AM Steven D'Aprano <steve@pearwood.info> wrote:
On Mon, Sep 14, 2020 at 12:16:54PM +1000, Chris Angelico wrote:

> (How often do people even use "x is True"?)

Alas, that is a common idiom. For example:

https://stackoverflow.com/questions/50816182/pep-8-comparison-to-true-should-be-if-cond-is-true-or-if-cond

It's possibly less common than it used to be now that PEP 8 warns
against it:

https://www.python.org/dev/peps/pep-0008/

and so linters flag it. But I've seen it used by beginners and
experienced programmers alike.

There is some justification for it in the case that you want to test
specifically for the True instance, being much shorter than:

    arbitrary_object == 1 and type(arbitrary_object) is bool

but many people have a mental blindspot and write:


    if flag is True:
        true_condition()
    else:
        false_condition()


even when they know full well that flag is guaranteed to be a bool.

I know that sometimes I catch myself doing the same. I recently
rediscovered some old Pascal code I write in the 1990s and sure enough,
I have "if flag == true" in that too.

Of course we all know that it should be written:

    if flag is True is True:
        ...

except I never know when to stop:

    if flag is True is True is True is True is True is True is ...


*wink*


--
Steve
_______________________________________________
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/45KTF4Z6EMXLSLN5PFXKS4QGIM3QEKGR/
Code of Conduct: http://python.org/psf/codeofconduct/