On 29.05.20 15:09, Chris Angelico wrote:

On Fri, May 29, 2020 at 10:51 PM Steven D'Aprano <st...@pearwood.info> wrote:
On Thu, May 28, 2020 at 08:04:07PM +1000, Chris Angelico wrote:
If it's a
language feature, then the name 'x' must be in the state of "local
variable without a value".
Oh ho, I see what you are doing now :-)

I'm going to stick with my argument that Python variables have two
states: bound or unbound. But you want to talk about the *meta-state* of
what scope they are in:

     LEGB = Local, Enclosing (nonlocal), Global, Builtin

There's at least one other case not captured in that acronym, Class
scope. There may be other odd corner cases.

In any case, if you want to distinguish between "unbound locals" and
"unbound globals" and even "unbound builtins", then I acknowledge that
these are genuine, and important, distinctions to make, with real
semantic differences in Python.
The reason locals are special is that you can't have a module-level
name without a value, because it's exactly the same as simply not
having one; but you CAN have a local name that must be exactly that
local, and you can't look up a module or builtin name, but it still
doesn't have a value. I believe local scope is the only one that
behaves this way.

Indeed locals are special, but why was it designed this way? Why not
resolve such an unbound local name in the enclosing scopes?

It seems that there is no way to modify locals once the function is
compiled (this is probably due to the fact that locals are optimized as
a static array?). For example:

    >>> x = 1
    >>> def foo():
    ...     exec('x = 2')
    ...     print(x)
    ...
    >>> foo()
    1

However in Python 2.7 this is possible:

    >>> x = 1
    >>> def foo():
    ...     exec('x = 2')
    ...     print(x)
    ...
    >>> foo()
    2
    >>> x
    1
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/NH4LP3YI5YDK7VNZKRZKZ4GOFFV7Z4XC/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to