Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.13] DOCS: Suggest always calling exec with a globals argument and no locals argument (GH-119235) #119239

Merged
merged 1 commit into from
May 20, 2024

Conversation

miss-islington
Copy link
Contributor

@miss-islington miss-islington commented May 20, 2024

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:

exec("""
def f():
    print("hi")

f()

def g():
    f()
g()
""", {}, {})

The reason not to leave out globals is as follows:

def t():
    exec("""
def f():
    print("hi")

f()

def g():
    f()
g()
    """)

(cherry picked from commit 7e1a130)

Co-authored-by: Hood Chatham roberthoodchatham@gmail.com


📚 Documentation preview 📚: https://cpython-previews--119239.org.readthedocs.build/

…ls argument (pythonGH-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 7e1a130)

Co-authored-by: Hood Chatham <roberthoodchatham@gmail.com>
@gpshead gpshead enabled auto-merge (squash) May 20, 2024 17:43
@gpshead gpshead merged commit fda3291 into python:3.13 May 20, 2024
31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir skip issue skip news
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants