-
-
Notifications
You must be signed in to change notification settings - Fork 32k
[doc] Questionable terminology ('free variables') for describing what locals() does #70870
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
Comments
The docs for locals() say that inside a function that local variables and free variables are included: https://docs.python.org/3/library/functions.html#locals Elsewhere we use the terms "cell variables" or "nonlocals". Mathematically, the word "free" means unbound, so that isn't applicable here and isn't the usual way of describing variables in the enclosing scope. >>> def f(x):
def g(y):
z = x + y
print(locals())
return g
>>> f(10)(20)
{'x': 10, 'y': 20, 'z': 30} Also, I'm not sure why "x" and "y" are included in the definition for locals(). That seems strange given that "x" and "y" are not local to "g" and that "x += 1" would fail with an UnboundLocalError. |
Regarding “free variables”, in bpo-17546 I proposed the wording “. . . also includes non-local, non-global names”. In your code example, I would consider y to be 100 percent local to the g() function. It is a function parameter, and “y += 1” should work. I agree x is not a true local, it is a “non-local non-global”. A national variable maybe :) For functions, considering that you shouldn’t modify the dictionary (original concern in bpo-17546), I do agree the behaviour is a bit strange and inconsistent. It might have made more sense to either only return true locals, or return a complete namespace of locals, non-locals, globals, and builtins. It seems there is no way to get a similar list of non-local non-globals in a class scope. |
I would think that "nonlocal" is exactly the right term given that that is how you would declare it if you wanted to write to it.
>>> def f(x):
def g(y):
nonlocal x
global w
z = x + y
x += 1
print(locals())
print(globals())
return g
>>> f(10)(20)
{'y': 20, 'x': 11, 'z': 30}
{'w': 5, ...} |
I requested that we stop (mis)using 'free variable' in the docs years ago. A strong +1 from me. The 'locals' function what named when 'local' and 'non-global' were synonyms. When non-local, non-global names were added, nonlocals were included with 'locals' as 'non-global'. (This must have been thought to be more useful than adding nonlocals() or excluding them.) They are, of course, local in some surrounding non-global context. And for most purposes, their entries in locals() should also be treated as read-only. I think the doc should say that function locals() includes the locals of surrounding function contexts, even though they are called 'nonlocal' within the nested function. |
The documentation [1] says: "If a variable is used in a code block but not defined there, it is a free variable." According to this description, it seems to me that the variable >>> def foo():
... print(x) But actually for the code object it is not:: >>> foo.__code__.co_freevars
() The meaning of free variable used for the code object is consistent with the >>> def foo():
... print(x)
... print(locals())
...
>>> x = 3
>>> foo()
3
{} So, I thing there is an inconsistency between the definition of "free variable" given in [1] and the meaning of "free variable" both in the code object and in the Instead, if we keep or remove the definition of "free variable" given in [1], I agree with Terry to change the So, at the end, to me the ideal solution would be to keep in [1] the current definition of "free variable", to change the [1] https://docs.python.org/3/reference/executionmodel.html#naming-and-binding |
Another point in the doc, where the meaning of "free variable" is inconsistent with the https://docs.python.org/3/reference/executionmodel.html#interaction-with-dynamic-features |
In 3.13, the
I also posted a separate PR after reviewing all the references to "free variable" in the documentation: #122545 While I left some of them alone, I changed several others to either "closure variable", or "free (closure) variable" (sometimes including specific references to The PR makes the definitions of |
The term "free variable" has unfortunately become genuinely ambiguous over the years (presumably due to the names of some relevant code object instance attributes). While we can't eliminate that ambiguity at this late date, we can at least alert people to the potential ambiguity by describing both the formal meaning of the term and the common alternative use as a direct synonym for "closure variable". --------- Co-authored-by: Carol Willing <[email protected]>
The term "free variable" has unfortunately become genuinely ambiguous over the years (presumably due to the names of some relevant code object instance attributes). While we can't eliminate that ambiguity at this late date, we can at least alert people to the potential ambiguity by describing both the formal meaning of the term and the common alternative use as a direct synonym for "closure variable". --------- (cherry picked from commit 2739099) Co-authored-by: Alyssa Coghlan <[email protected]> Co-authored-by: Carol Willing <[email protected]>
…125088) The term "free variable" has unfortunately become genuinely ambiguous over the years (presumably due to the names of some relevant code object instance attributes). While we can't eliminate that ambiguity at this late date, we can at least alert people to the potential ambiguity by describing both the formal meaning of the term and the common alternative use as a direct synonym for "closure variable". --------- (cherry picked from commit 2739099) Co-authored-by: Alyssa Coghlan <[email protected]> Co-authored-by: Carol Willing <[email protected]>
The term "free variable" has unfortunately become genuinely ambiguous over the years (presumably due to the names of some relevant code object instance attributes). While we can't eliminate that ambiguity at this late date, we can at least alert people to the potential ambiguity by describing both the formal meaning of the term and the common alternative use as a direct synonym for "closure variable". --------- Co-authored-by: Carol Willing <[email protected]>
Uh oh!
There was an error while loading. Please reload this page.
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
Linked PRs
The text was updated successfully, but these errors were encountered: