3 ms·
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():
by eesmith 2y ago
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.
- mg 2y agoThat'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 https://x.com/marekgibney/status/1817495719520940236 and https://x.com/marekgibney/status/1817495721974583569 https://x.com/marekgibney/status/1817495721974583569
- deleted 2y ago[deleted]
- eesmith 2y agoI 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.