Comparing boolean values with ==
works in Python. But when I apply the boolean not
operator, the result is a syntax error:
Python 2.7 (r27:82500, Sep 16 2010, 18:02:00)
[GCC 4.5.1 20100907 (Red Hat 4.5.1-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> True == True
True
>>> False == False
True
>>> True is not False
True
>>> True == not False
File "<stdin>", line 1
True == not False
^
SyntaxError: invalid syntax
>>>
Why is this a syntax error? I would expect not False
to be an expression that returns a boolean value, and True == <x>
to be valid syntax wherever <x>
is an expression with valid syntax.
True == not
is the actual syntax error, anything after that is irrelevant. – dansalmo Dec 28 '13 at 23:39not
, regardless of the types compared.True < not False
,3 <= not 2
,'Foo' > not 'False'
,3.3 >= not 4.5
,{} is not not []
,set() == not None
andslice() != not lambda: x
all raise the same syntax error. This is not limited to== not
and booleans. – Martijn Pieters♦ Jan 31 '14 at 17:12