True is True / False is False?
On Wed, Jul 22, 2020 at 11:04 AM Tim Chase
<python.list at tim.thechases.com> wrote:
> I know for ints, cpython caches something like -127 to 255 where `is`
> works by happenstance based on the implementation but not the spec
> (so I don't use `is` for comparison there because it's not
> guaranteed by the language spec). On the other hand, I know that None
> is a single object that can (and often *should*) be compared using
> `is`. However I spent some time reading through the language specs and
> didn't encounter anything about booleans returned from
> comparisons-operators, guaranteeing that they always return The One
> True and The One False.
| bool(x) -> bool
| Returns True when the argument x is true, False otherwise.
| The builtins True and False are the only two instances of the class bool.
| The class bool is a subclass of the class int, and cannot be subclassed.
The docstring (shown here in "help(bool)") says clearly that you'll
only ever have those two instances and no others, so yes, it's a
language guarantee that you'll always get the exact same objects.
That said, though, a comparison isn't required to return a bool. If it
*does* return a bool, it has to be one of those exact two, but it
could return anything it chooses. But for built-in types and most
user-defined types, you will indeed get a bool.