True
and False
are singleton objects.
True
is nothing more than a label that points to some object in memory that is a bool type, lives at some memory address and it has internal value of integer 1. False
is the same thing which has an interval value of 0.
Because they are singleton objects, they will always retain their same memory address throughout the lifetime of your program.
Python has bool
class that used to represent Boolean values. However, the bool class is a subclass of the int class. Since bool
objects are also int
objects, they can also be interpreted as the integers 1 and 0
int(True) -> 1
int(False) ->0
IMPORTANT to understand, True
and 1 are not same objects. True
lives at some memory address and ` lives at some different memory address. we compare their references here:
id(True) != id(1)
Or
True is 1 -> False
Because identity operator checks the memory address
Passing values
True > False --> True
This is strange behavior. In this case, since True holds 1 and False holds 0
1 > 0
another example
a=True+True+True
a will be 3. it seems right we are passing values but everything is passed by reference - it's just that for singleton objects it's the same reference.