diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2024-05-20 18:11:36 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-20 18:11:36 (GMT) |
commit | a50f0651eeba115f82455a9e48178764bace35eb (patch) | |
tree | 528c8a02c0c3873ade020162d7c0c11dc1e4cff2 /Doc/library/argparse.rst | |
parent | 541b89eaf606de286bc7c33588b63c3d0a9310b8 (diff) | |
download | cpython-a50f0651eeba115f82455a9e48178764bace35eb.zip cpython-a50f0651eeba115f82455a9e48178764bace35eb.tar.gz cpython-a50f0651eeba115f82455a9e48178764bace35eb.tar.bz2 |
[3.12] DOCS: Suggest always calling exec with a globals argument and no locals argument (GH-119235) (#119240)
DOCS: Suggest always calling exec with a globals argument and no locals argument (GH-119235)
Many users think they want a locals argument for various reasons but they do not
understand that it makes code be treated as a class definition. They do not want
their code treated as a class definition and get surprised. The reason not
to pass locals specifically is that the following code raises a `NameError`:
```py
exec("""
def f():
print("hi")
f()
def g():
f()
g()
""", {}, {})
```
The reason not to leave out globals is as follows:
```py
def t():
exec("""
def f():
print("hi")
f()
def g():
f()
g()
""")
```
(cherry picked from commit 7e1a130b8ff1ed8b3a5f00fe0f06d3916b852216)
Co-authored-by: Hood Chatham <roberthoodchatham@gmail.com>
Diffstat (limited to 'Doc/library/argparse.rst')
0 files changed, 0 insertions, 0 deletions