3 ms·
> I'm curious why this is reported as a SyntaxError. "Names listed in a global statement must not be used in the same code block textually preceding that globa
by dragonwriter 7y ago
> I'm curious why this is reported as a SyntaxError.
"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#grammar-token-global-stmt https://docs.python.org/3/reference/simple_stmts.html#gramma...
- lilyball 7y agoIf you can construct an AST, you’ve moved past the syntax stage.
- dbcurtis 7y agoWell, in a pure sense, you are correct. But for practical reasons, it can sometimes be easier to define a parsing grammar that is a little loose, and pick up the stragglers by analyzing the AST. Sometimes the “correct” grammar is needlessly complex and you can reduce the headaches with a quick sanitation pass over the results of a less pure parser.
- lilyball 7y agoEven so, I wouldn't consider this a syntax error, because the error isn't with the syntax but with using an identifier in a context where that particular identifier isn't allowed (but others are). This would make it more akin to a NameError.