Timur Sharapov Timur Sharapov - 13 days ago 6
Python Question

Boolean operations in Python (notation in the docs)

I'm not a novice programmer and I know what booleans are and how to use them. Trouble is, I can't quite understand what is implied in Python's documentation regarding boolean operations:

or_test ::= and_test | or_test "or" and_test
and_test ::= not_test | and_test "and" not_test
not_test ::= comparison | "not" not_test


What does all this recursion mean? Why should there be an "and_test" inside of an or_test?
The same question about "not_test" in the description of and_test
The third line makes even less sense to me.
Could anyone please guide me through these lines so that I can finally understand this somewhat unnecessarily recursive and tangled notation

Answer

This is part of the larger language grammar and in essence specifies the operator precedence.

It means that and binds tighter than or does. Note that or_test does not have to contain and, it contains the and_test rule, which is constructed from either an not_test rule or and_test "and" not_test. not_test itself constructed in a similar manner, etc.

Also see the Notation section of the reference documentation.

Comments