Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-70870: Clarify dual usage of 'free variable'#122545

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
ncoghlan merged 10 commits intopython:mainfromncoghlan:gh-70870-co_freevars-docs
Oct 8, 2024

Conversation

ncoghlan
Copy link
Contributor

@ncoghlanncoghlan commentedAug 1, 2024
edited
Loading

  • Defineclosure variable andfree variable in glossary
  • Usefree (closure) variable to link to theclosure variable term when describing
    API elements that use "free" in the narrower "closure variable" sense rather than
    the broader formally defined sense of any reference to names other than those of
    local variables
  • Switch entirely to theclosure variable term when describing API elements that
    don't otherwise make any reference to the term "free"

Closes#70870
Closes#98117


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

@ncoghlanncoghlan added docsDocumentation in the Doc dir triagedThe issue has been accepted as valid by a triager. 3.13bugs and security fixes 3.14bugs and security fixes needs backport to 3.13bugs and security fixes labelsAug 1, 2024
@encukou
Copy link
Member

encukou commentedAug 2, 2024
edited
Loading

Now, whenever you readfree variable, you're left wondering about which of the two meanings is meant. Can we avoid that?

Would it make sense to accept the “pragmatic” meaning offree variable?

[edit: Oh, I understood this wrong: the distinction is in globals/builtins, not in variables used in an inner scope, right?]
..., and in the few cases that exclude variables only accessed in inner nested scopes, say that explicitly?

After all, “used” inexecutionmodel.html's definition is itself not too specific. Does anonlocal without “actual” use count? Does adef statement that binds a cell from an outer scope count?

@ncoghlan
Copy link
ContributorAuthor

ncoghlan commentedAug 3, 2024
edited
Loading

Now, whenever you readfree variable, you're left wondering about which of the two meanings is meant. Can we avoid that?

Would it make sense to accept the “pragmatic” meaning of free variable?

We can't, as the formal meaning isn't specific to Python.

The heart of the problem is that the use of the closure/free name pairing when cell variables were introduced was formally incorrect, since the named free variables were potentially only a subset of all free variables if a nested function also referenced global or builtin names. However, the docs haven't historically made that clear, they just called them free variables, so folks inferring the meaning of the phrase from the way those parts of the CPython docs used it would understandably conclude that it referred specifically to references to closure variables (e.g. I had to go read the code to determine whether thesymtable docs were using "free" in its formal sense or its pragmatic sense, and it turned out to be the latter).

That naming decision was made decades ago though, so the best we can reasonably do is:

  1. acknowledge that the phrase "free variable" is now unavoidably ambiguous at least as far as CPython is concerned, and to some degree for Python in general
  2. indicate that some contexts (such as the formal language spec) always use one of the two meanings so it doesn't need to be spelled out on each use (this mostly applies to cases where "free variable" is being used correctly in the formal sense, since I didn't want to modify the related wording in the execution model docs as part of this PR)
  3. where practical, update the wording of affected documentation to remove the ambiguity (this mostly involved either adding a "closure" qualifier or explicitly mentioning that globals and builtins were excluded when the term is being used in its pragmatic sense)

And yeah, references to variables in outer scopes are included in both the formal and pragmatic uses of the phrase. The difference is just that the pragmatic use omits any references to global and builtin names, since the compiler doesn't actually keep track of those outside the individual lookup opcodes, so the only way to get that list is to scan the full AST or opcode array for the code block.

(While the ambiguity is unfortunate, the situation isn't bad enough to justify making implementation changes solely due to this)

encukou reacted with thumbs up emoji

@encukou
Copy link
Member

One more idea that might be off the mark: how about usingnonlocal rather thanclosure variable?
The keyword already can't be used for global/built-in variables ( nonlocal scopes are the local scopes of the enclosingfunctions, so it's not a synonym for “non-local”). Some variables are nonlocal implicitly.

@ncoghlan
Copy link
ContributorAuthor

We usenonlocal in thesymtable API to specifically mean "marked with the nonlocal keyword", rather than as a general term for all closure variables.

The function object attribute is also called__closure__ rather than__nonlocals__, as is the parameter toexec andeval, so there's no getting away from closure variables being a term worth defining.

@ncoghlanncoghlan marked this pull request as draftSeptember 13, 2024 09:04
@ncoghlan
Copy link
ContributorAuthor

(Converted to draft, since I don't know when I'll get back to this and incorporate Petr's feedback)

@JelleZijlstra
Copy link
Member

I read the PR and all makes sense to me. Happy to take another look after you incorporate Petr's feedback.

ncoghlan reacted with thumbs up emoji

@ncoghlanncoghlan marked this pull request as ready for reviewOctober 7, 2024 13:08
Copy link
Contributor

@willingcwillingc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Docs look good to me@ncoghlan. Thanks for the many clarifications.

Co-authored-by: Carol Willing <carolcode@willingconsulting.com>
@ncoghlan
Copy link
ContributorAuthor

Thanks for the reviews, folks!

@ncoghlanncoghlanenabled auto-merge (squash)October 8, 2024 07:34
@ncoghlanncoghlan merged commit2739099 intopython:mainOct 8, 2024
23 checks passed
@miss-islington-app
Copy link

Thanks@ncoghlan for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull requestOct 8, 2024
The term "free variable" has unfortunately become genuinelyambiguous over the years (presumably due to the names ofsome relevant code object instance attributes).While we can't eliminate that ambiguity at this late date, we canat least alert people to the potential ambiguity by describingboth the formal meaning of the term and the commonalternative use as a direct synonym for "closure variable".---------(cherry picked from commit2739099)Co-authored-by: Alyssa Coghlan <ncoghlan@gmail.com>Co-authored-by: Carol Willing <carolcode@willingconsulting.com>
@bedevere-app
Copy link

GH-125088 is a backport of this pull request to the3.13 branch.

@bedevere-appbedevere-appbot removed the needs backport to 3.13bugs and security fixes labelOct 8, 2024
ncoghlan added a commit that referenced this pull requestOct 8, 2024
…125088)The term "free variable" has unfortunately become genuinelyambiguous over the years (presumably due to the names ofsome relevant code object instance attributes).While we can't eliminate that ambiguity at this late date, we canat least alert people to the potential ambiguity by describingboth the formal meaning of the term and the commonalternative use as a direct synonym for "closure variable".---------(cherry picked from commit2739099)Co-authored-by: Alyssa Coghlan <ncoghlan@gmail.com>Co-authored-by: Carol Willing <carolcode@willingconsulting.com>
efimov-mikhail pushed a commit to efimov-mikhail/cpython that referenced this pull requestOct 9, 2024
The term "free variable" has unfortunately become genuinelyambiguous over the years (presumably due to the names ofsome relevant code object instance attributes).While we can't eliminate that ambiguity at this late date, we canat least alert people to the potential ambiguity by describingboth the formal meaning of the term and the commonalternative use as a direct synonym for "closure variable".---------Co-authored-by: Carol Willing <carolcode@willingconsulting.com>
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@encukouencukouencukou left review comments

@willingcwillingcwillingc approved these changes

Assignees
No one assigned
Labels
3.13bugs and security fixes3.14bugs and security fixesdocsDocumentation in the Doc dirtriagedThe issue has been accepted as valid by a triager.
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

Improved documentation for exec() closure parameter [doc] Questionable terminology ('free variables') for describing what locals() does
4 participants
@ncoghlan@encukou@JelleZijlstra@willingc

[8]ページ先頭

©2009-2025 Movatter.jp