On 2021-03-04 03:39, Avi Gross via Python-list wrote:
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.
If you look through the grammar, the relevant bits are:

    sum: sum '+' term
    term: factor
    factor: power
    power: await_primary
    await_primary: primary
    primary: atom

and an 'atom' can be a number, etc, but can never start with "not".

A 'primary' can be an 'atom' and can have a subscript.

-----Original Message-----
From: Python-list <python-list-bounces+avigross=verizon....@python.org> On 
Behalf Of Rob Cliffe via Python-list
Sent: Wednesday, March 3, 2021 10:19 PM
To: Python <python-list@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

Reply via email to