Identity comparison between booleans and integers

Viewed 51

I've been playing and found this.

The operators is and is not test for an object’s identity: x is y is true if and only if x and y are the same object. An Object’s identity is determined using the id() function. x is not y yields the inverse truth value.

>>> list(map(id, [0, 1, True, not False, False, not True]))
[94660352164256, 94660352164288, 94660351988128, 94660351988128, 94660351988096, 94660351988096]

Right now everything is ok: 0, 1, True and False have different ids, as they are different objects. And they are:

>>> True is 1
False
>>> False is 0
False
>>> not False is True
True
>>> not True is False
True

But then:

>>> not False is 1
True
>>> not True is 0
True
>>> 

My question is: How is it possible that at the same time True is not 1, not False is True and not False is 1 all return True?

2 Answers

It has to do with priorities & operator precedence. Think of it like this:

(True is not 1), not (False is True), not (False is 1).

Now everything should make sense.

Related