a + not b

Avi Gross avigross at verizon.net
Wed Mar 3 22:39:49 EST 2021


As a guess, Rob, precedence rules for not may not bind as strongly as you think.

1 + (not 1)

With parentheses, "not 1" is a subexpression that should be performed first and might return the value "False"

1 + False

treats False in a numeric context as a zero so evaluates to 1.

But

1 + not 1 

Looks a bit ambiguous as "+ " expects something it considers a number or that can be converted to a number. Without parentheses, it may try to do 

1 + not

Which gets the same Syntax error.

I looked and operator "not" is evaluated much later than "+" and just before "and" and "or" so you need parentheses to force the interpretation you may intend. Similarly, some used of "and" require parentheses as do other operators.

-----Original Message-----
From: Python-list <python-list-bounces+avigross=verizon.net at python.org> On Behalf Of Rob Cliffe via Python-list
Sent: Wednesday, March 3, 2021 10:19 PM
To: Python <python-list at python.org>
Subject: a + not b

I can't work out why
     1 + - 1
     1 + (not 1)
are legal syntax, but
     1 + not 1
isn't.
Is there a good reason for this?
Thanks
Rob Cliffe

-- 
https://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list