The constants True
and False
are unique, as the specification guarantees they are the only instances of bool
. That is, if you have two variables which were both initialized with True
or a true boolean expression such as 1 == 1
,* then they will compare identical with is
. The same is true for False
.
However, True == 1 and True is not 1
. This is because booleans are a subclass of int
. Boolean values will never compare identical to "regular" integers with is
, but the type difference is ignored for comparison with integers, floating point values, and other numeric types, as is standard behavior for the numeric hierarchy.
* Be careful with more elaborate boolean expressions. In particular, and
and or
always return one of their operands, and do not coerce to boolean (unless their operands are already booleans).
(Incidentally, Python contains exactly two other singletons in addition to True
, False
, and None
, namely Ellipsis
and NotImplemented
. Each type object is also more or less unique; if you write x = int; y = int
, then x
and y
will compare identical with is
, because there is only one object which represents the int
type. This can be used if you are creating your own type hierarchy and want to avoid having objects compare equal to their subclasses like booleans do with integers. In other cases, it is of marginal benefit compared to isinstance()
or issubclass()
, which better respect the Liskov substitution principle.)