[Python-Dev] [SPAM?] Re: PEP 558: Defined semantics for locals()

Steven D'Aprano steve at pearwood.info
Sun May 26 06:04:11 EDT 2019


Richard, your email seems to have introduced a spurious "SPAM" label
to this thread, which may confuse some email clients into treating it
as spam. Can you teach your email program that this mailing list is ham,
not spam, or failing that, at least edit the subject line to remove the
label? Thanks. I've done so for this response, but please take care that 
you don't re-introduce the label again, thanks.


On Sat, May 25, 2019 at 06:37:22PM -0400, Richard Damon wrote:

> To me that is a static snapshot of a dynamic environment, not a dynamic
> snapshot. The snapshot you get at THAT moment in time won't change, as
> time progresses, so that snapshot itself isn't dynamic.

Actually, it does change -- but the confusing part is that it doesn't 
change automatically but only when you call the locals() function again. 
This already CPython's behaviour, so that is not changing.

def demo1():
    a = b = c = 1
    a = locals()
    print(a)
    b = 999
    print(a)

def demo2():
    a = b = c = 1
    a = locals()
    print(a)
    b = 999
    locals()  # call function but throw the result away
    print(a)


And running those two functions in Python 3.5:

py> demo1()  # No change to the dict.
{'a': 1, 'b': 1, 'c': 1}
{'a': 1, 'b': 1, 'c': 1}

py> demo2()  # Dict magically updates!
{'a': 1, 'b': 1, 'c': 1}
{'a': {...}, 'b': 999, 'c': 1}


I know this is the backwards-compatible behaviour, but I would like to 
question whether we want to enshrine it in stone. This seems to me to 
be the worst possible combinations of features:

- writing to the locals() dict doesn't write changes back to the 
  actual local variables;

- the dict returned isn't a fixed snapshot, but the PEP calls it a 
  snapshot despite not being one (naming things is hard);

- the "snapshop" can change as a side-effect of another operation.


If this wasn't already the behaviour, would we want it?



-- 
Steven


More information about the Python-Dev mailing list