Hacker News new | past | comments | ask | show | jobs | submit login

Yes, I stumbled across this quite recently:

https://x.com/marekgibney/status/1817495719520940236

I was surprised that you cannot access dynamically created local variables even though they exist.

Looks like the local dictionary still exists. Just that whether accessing a local variable looks it up in the local or global dictionary is determined at compile time.




See PEP 667, "Consistent views of namespaces" https://peps.python.org/pep-0667/ .

In Python 3.12, quoting https://docs.python.org/dev/whatsnew/3.13.html

> PEP 667: The locals() builtin now has defined semantics when mutating the returned mapping. Python debuggers and similar tools may now more reliably update local variables in optimized scopes even during concurrent code execution.

with more details at https://docs.python.org/dev/whatsnew/3.13.html#whatsnew313-l... , which adds:

> To ensure debuggers and similar tools can reliably update local variables in scopes affected by this change, FrameType.f_locals now returns a write-through proxy to the frame’s local and locally referenced nonlocal variables in these scopes


Interesting. As I understand it, it will not affect my example:

    def f():
        exec('y = 7') # will still create a local variable
        print(locals()['y']) # will still print this local variable
        print(y) # will still try to access a global variable 'y'


That does not work for me in Python 3.10 or 3.12:

  Python 3.12.2  ...
  Type "help", "copyright", "credits" or "license" for more information.
  >>> def f():
  ...         exec('y = 7') # will still create a local variable
  ...         print(locals()['y']) # will still print this local variable
  ...         print(y) # will still try to access a global variable 'y'
  ...
  >>> f()
  7
  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "<stdin>", line 4, in f
  NameError: name 'y' is not defined
It does print 7 in Python 2.7.


That's what I meant. It tries to access a global variable 'y' and fails because there is none.

See also:

https://x.com/marekgibney/status/1817495719520940236

and

https://x.com/marekgibney/status/1817495721974583569


I see what you mean now.

Messing with locals has been a long-time danger zone. I don't know how it is supposed to interact with exec - another danger zone - and I don't know enough about the changes in 3.13 to know if it's resolved.




Consider applying for YC's W25 batch! Applications are open till Nov 12.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: