A tiny Python exception oddity
aroberge.blogspot.com
aroberge.blogspot.com
Jeepers, who WOULD be qualified to explain for sure how this works?
Guido?
[1] https://www.google.com/search?q=how+many+python+programmers+...
Here, the production is func_body_suite → stmt+ → (stmt, stmt) → (simple_stmt, simple_stmt) → (small_stmt, small_stmt) → (global_stmt, nonlocal_stmt) ; syntactically, I'm inclided to agree that the code sample seems valid. (It is, of course, semantically malformed.)
I suspect that there just never was a separate exception type for this sort of thing.
"Names listed in a global statement must not be used in the same code block textually preceding that global statement"; the nonlocal declaration is a use, and so that syntax rule is violated.
https://docs.python.org/3/reference/simple_stmts.html#gramma...
For some examples:
x == not y
Rejected by the parser itself. not x = y
Accepted by the parser and AST builder, and only rejected by the bytecode compiler. f(*xs,)
Rejected by the parser itself. ...
Rejected somewhere between the parser and the AST builder. Valid uses of Ellipsis are actually special-cased; it is not a standard part of the abstract syntax.[0] https://github.com/python/cpython/blob/master/Parser/Python....
https://docs.python.org/3.8/reference/grammar.html
The RHS of the == (comp_op) has to be an expr. You can force "not x" to be an expr by parenthesizing it:
x == (not x) not x == x
is totally valid.That is what makes it a syntax error.
I am not a "Programmer" so having feedback on the original problem line would be more beneficial than returning the second.