News for hackers. Stuff that matters.
Re:What’s New in Python 3.8 by edflsafoiewq on Monday October 14, 2019 @10:14PM (#21254985)
I'd really like a way to refer to a variable in a outer scope (like global... but not just for global variables).
Re:What’s New in Python 3.8 by paultudorjones on Monday October 14, 2019 @10:26PM (#21255047)
Re:What’s New in Python 3.8 by eru on Tuesday October 15, 2019 @02:16AM (#21255961)
In the same vein, they could add syntax to distinguish introducing a variable from setting the value of an existing variable.

Python's heuristics for that are pretty annoying.

Re:What’s New in Python 3.8 by arnsholt on Tuesday October 15, 2019 @04:19AM (#21256518)
I think that feature has exactly zero chances of getting adopted, but that's probably one of my top Python annoyances too.

If it's any consolation, the scope of a variable isn't determined by heuristics, it's just that the rules are (IMO) kinda bad. Basically, if a variable is assigned (in addition to `=` and friends, `import`, `class` and `def` are also assignments in disguise) to in what Python calls a "block" (which is not like a C-style block; rather `class` body, `def` body, or top-level) it is considered to belong to that block (with the notable exception of the name for a caught exception in a `except Exception as e: ...`, see [1]). If you want it to refer to a variable outside the block, you need to use `global` or `nonlocal` as appropriate. The full gory details are in [2]

1: https://docs.python.org/3/reference/compound_stmts.html#the-...

2: https://docs.python.org/3/reference/executionmodel.html#nami...

Re:What’s New in Python 3.8 by eru on Tuesday October 15, 2019 @06:12AM (#21257071)
Yes, I meant the rules are bad. I called them heuristics, because I assume they came up with the rules as a heuristic to what would be the most useful behaviour.

Python didn't use to have lexical scoping, so the rules are retrofitting around that.

Re:What’s New in Python 3.8 by arnsholt on Tuesday October 15, 2019 @07:54AM (#21257540)
Yeah, it all seems a bit hacky. Especially the bit where a caught exception bound to a variable is cleared at the end of the `except` clause. I'm pretty sure it's because if you didn't you'd get a reference cycle (exception object -> stack trace -> function invocation record -> function locals -> exception object), which makes CPython's GC sad.
Re:What’s New in Python 3.8 by alanfranz on Tuesday October 15, 2019 @02:46AM (#21256107)
... and what prevents you from doing that? You mean you'd want to reassign them?