Skip to content

Conversation

@Vemulakonda559
Copy link

@Vemulakonda559 Vemulakonda559 commented Nov 29, 2025

This PR adds a clarification to the documentation of function.__closure__,
noting that only variables from an enclosing scope that are actually
referenced in the inner function are included in its closure.

Names defined in the outer function but not referenced inside the nested
function do not appear in co_freevars and therefore do not contribute
cells to function.__closure__.

This behavior follows from the definition of free variables in the
execution model, but placing this clarification directly in the
function.__closure__ section improves interpretability for readers
examining closure behavior.

Documentation-only change; no behavioral modifications.


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

@python-cla-bot
Copy link

python-cla-bot bot commented Nov 29, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app bedevere-app bot added docs Documentation in the Doc dir skip news labels Nov 29, 2025
@github-project-automation github-project-automation bot moved this to Todo in Docs PRs Nov 29, 2025
@Vemulakonda559

This comment was marked as resolved.

1 similar comment
@Vemulakonda559

This comment was marked as resolved.

@Vemulakonda559

This comment was marked as resolved.

@picnixz

This comment was marked as resolved.

@picnixz

This comment was marked as resolved.

@picnixz
Copy link
Member

picnixz commented Nov 29, 2025

Ok, after searching a bit more, we could just link the glossary term: "closure variable" which includes this clarification.

@picnixz picnixz changed the title doc: clarify which variables appear in function.__closure__ gh-142093: clarify which variables appear in function.__closure__ Nov 29, 2025
@picnixz
Copy link
Member

picnixz commented Nov 29, 2025

Sorry for telling you to create an issue. I thought it needed more discussion but I think we can just add a reference to the closure variable or free variable term instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review docs Documentation in the Doc dir skip news

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

2 participants