I know that Python guarantees that there is only one instance of NoneType
, the None
object, so that you can safely use is None
to test if something equals None
.
Is there an equivalent guarantee for bool
True
and False
(i.e. that there is only one instance of each)?
If not, why not?
EDIT: In particular, I've noticed that (n+0) is (0+n)
gives True
for n in range(-5, 257)
and False
otherwise. In other words, zero, the first 256 positive and the first 5 negative integers seem to be pre-cached and are not instanced again. I am guessing that that's a choice of the interpreter (CPython, in my case) and not a specification of the language. And bool
derives from int
, so I still have to wonder about what expectations I can have with other interpreters.
EDIT: To clarify, since this seems to have generated a lot of confusion, my intention is not to test the boolean interpretation of a value. For that I would never use is True
or is False
. My intention is to be able to tell apart False
from everything else, in a variable that can have values of several types including empty strings, zeros, and None
, and similarly for True
. I'm myself an experienced programmer, of the kind who cringes when I see "if booleanvar == True".
NOTE ON DUPLICATES: The questions this one was alleged to be a duplicate of (this and this) don't answer this question; they merely state that bool
is a subclass of int
that differ mainly in their repr
, not if True
and False
are guaranteed to be unique.
Also, note that it's not a question about what the names True
and False
are bound to, but about the instances of the class bool
.