2 ms·
1 and 2 are the scoping rules (since Python has no variable declarations [1], making all blocks scopes would be pretty awkward and require tons of "xy=None # r
by formerly_proven 3y ago
1 and 2 are the scoping rules (since Python has no variable declarations [1], making all blocks scopes would be pretty awkward and require tons of "xy=None # reassigned later" and nonlocals), 3 should result in [2, 3, 4], not [].
[1] It actually does nowadays, ever since non-assigning annotations came around.
- colejohnson66 3y ago> ... making all blocks scopes would be pretty awkward and require tons of "xy=None # reassigned later" and nonlocals) ... C, C++, Java, C#, JS/TS (through let), Rust, and more all require variable declaration before use. Python is the odd one out here. JavaScript had something similar (variable hoisting), but it's so problematic that we ditched it for let/const.