Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork32.2k
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
Uh oh!
There was an error while loading.Please reload this page.
Conversation
encukou commentedAug 2, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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?]
|
ncoghlan commentedAug 3, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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 the That naming decision was made decades ago though, so the best we can reasonably do is:
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) |
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
One more idea that might be off the mark: how about usingnonlocal rather thanclosure variable? |
We use The function object attribute is also called |
(Converted to draft, since I don't know when I'll get back to this and incorporate Petr's feedback) |
I read the PR and all makes sense to me. Happy to take another look after you incorporate Petr's feedback. |
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
There was a problem hiding this 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.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Co-authored-by: Carol Willing <carolcode@willingconsulting.com>
Thanks for the reviews, folks! |
2739099
intopython:mainUh oh!
There was an error while loading.Please reload this page.
Thanks@ncoghlan for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13. |
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>
GH-125088 is a backport of this pull request to the3.13 branch. |
…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>
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>
Uh oh!
There was an error while loading.Please reload this page.
closure variable
andfree variable
in glossaryfree (closure) variable
to link to theclosure variable
term when describingAPI 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
closure variable
term when describing API elements thatdon't otherwise make any reference to the term "free"
Closes#70870
Closes#98117
📚 Documentation preview 📚:https://cpython-previews--122545.org.readthedocs.build/