为什么 python 允许在布尔运算符前面省略 space 但后面不允许?

Why does python allow omitting space in front of boolean operator but not after?

这个有效: 1and 1 并评估为 1.

1 and1 抛出 SyntaxError。

为什么前面的space是可选的,后面的space不是?

根据 lexical analysis:

Whitespace is needed between two tokens only if their concatenation could otherwise be interpreted as a different token...

and1 是有效的 identifier,因此 and1 可以解释为单个标记,而 1and 不是,所以不能。

大概至少部分是因为这个原因,PEP-8 recommends 总是用空格包围它们:

Always surround these binary operators with a single space on either side: assignment (=), augmented assignment (+=, -= etc.), comparisons (==, <, >, !=, <>, <=, >=, in, not in, is, is not), Booleans (and, or, not).