]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
[3.13] DOCS: Suggest always calling exec with a globals argument and no locals argume...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 20 May 2024 18:15:25 +0000 (20:15 +0200)
committerGitHub <noreply@github.com>
Mon, 20 May 2024 18:15:25 +0000 (18:15 +0000)
commitfda3291643827d2c89ee1d5cc9b9b65c3e83d1e8
tree063a27940f73330a51b2eb596e9980b5e9e2d14d
parenta6b873fb54a62d2725ad38ad6d02bb63a85f605f
[3.13] DOCS: Suggest always calling exec with a globals argument and no locals argument (GH-119235) (#119239)

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>
Doc/library/functions.rst