3 ms·
One thing it misses is compile-time checks for e.g. the format spec.
by amelius 2y ago
One thing it misses is compile-time checks for e.g. the format spec.
- karamanolev 2y agoDoesn't all of Python miss that, having (close to) no compile time?
- amelius 2y agoPython does some checks before it runs code. E.g.: print("hello") def f(): nonlocal foo gives: SyntaxError: no binding for nonlocal 'foo' found before printing hello, and note that f() wasn't even called.
- nomel 2y agoI think it's just giving an error because a valid AST can't be made, which means valid bytecode can't be made. "<word> <word>" is only valid syntax if one is a reserved word. `nonlocal(foo)` is just fine, of course.
- pansa2 2y ago> "<word> <word>" is only valid syntax if one is a reserved word. `nonlocal` is a keyword
- zahlman 2y agoNo, it gives an error because `nonlocal foo` requests that the name `foo` be looked up in a closure, but `f` doesn't have such a closure (the `foo` defined outside the function is global instead). `nonlocal` is the same sort of keyword as `global` but for enclosing functions instead of the global namespace; see also https://stackoverflow.com/questions/1261875 https://stackoverflow.com/questions/1261875 .
- nomel 2y agoHere's the statement checking code, which I believe is pre-AST [1]. I would have to dig more to see if that check is there to prevent invalid AST or to just "help the user" (would depend on how they reference the original variable I suppose). But wow, that's the first time I've seen "nonlocal". In the ~100 packages I have installed, I see 0 usages! [1] https://github.com/python/cpython/blob/a6a3dbb7db0516a72c5ef149223d904f4e6a31ae/Python/ast.c#L714 https://github.com/python/cpython/blob/a6a3dbb7db0516a72c5ef...
- zahlman 2y agoWell, yes, not a lot of people write closures except perhaps when they implement decorators. So there's ordinarily no non-local scope to worry about. People tend to write classes instead, because that's what's familiar.
- pauleveritt 2y agoDo t-strings miss something that f-strings provides for format_spec etc.? FWIW, format_spec is available in the template structure, so the function writer could at least do a runtime check.