
On Fri, May 29, 2020 at 2:18 PM Steven D'Aprano <steve@pearwood.info> wrote:
On Thu, May 28, 2020 at 12:11:38PM +0200, Alex Hall wrote:
Consider this code:
``` x = 1
def foo(): print(x) x = 2
foo() ```
Here `print(x)` doesn't print '1', it gives `UnboundLocalError: local variable 'x' referenced before assignment`. It knows that `x` is meant to be a local and ignores the global value. That doesn't look like an implementation detail to me - does Jython do something different?
I never said that Python's scoping rules were implementation details.
I said that the storage mechanism of *how* local variables are stored, and hence whether or not writes to `locals()` are reflected in the local variables, is an implementation detail.
I didn't know what it was you were saying back then, and I'm trying, but I still haven't figured it out. I understand bits of it, but I don't know what larger point you're trying to make. I think there are some things you need to try harder to communicate clearly. Chris sad:
There is no *value* in x, yet x has a state.
You responded:
In Python code, no, it has no state, it's just an unbound name.
and then started talking about the locals() dict in great detail for reasons I haven't grasped. My point with that code snippet is that Python (not just some implementations) can distinguish between a bound local, an unbound local, a bound variable of some other type, and a name that isn't defined at all. I think labeling those as 'states' is pretty reasonable. It seemed pretty clear to me that that's the kind of thing Chris was talking about. Maybe you disagree with that label, or are trying to make some other distinction, but I don't see the relevance, as the word 'state' isn't the point anyway. Chris was just saying that a statement like `x ?= y` meaning 'assign the value of y to the local variable x if x is currently unbound' already fits neatly into the language model and doesn't require major changes. Do you disagree? (although I don't endorse the proposal for `x ?= y`)