Questions about `locals` builtin

dieter dieter at handshake.de
Tue Feb 27 01:52:46 EST 2018


Kirill Balunov <kirillbalunov at gmail.com> writes:
> I am a little bit confused with `locals` builtin in these moments:
>
> 1. The documentation says that _free varaibles_ are returned, which seems
> incorrect description. In my mind the term free variable refers to
> variables used in a function that are not local variables nor parameters of
> that function.
>
>     In docs: "Update and return a dictionary representing the current local
> symbol table. Free variables are returned by `locals()` when it is called
> in function blocks, but not in class blocks."

I agree with your definition of "free variable" and I think that
the documentation partially speaks of it.

In a function body, you have two kinds of "free variables":
implicitly used free variables and explicitly declared (via "global",
"nonlocal") free variables.

I think the documentation wants to make clear, that "locals"
does not treat "free variables" in a completely consistent way
(there is a difference between class and function blocks).

I assume that "locals" may return some (but not necessary all)
free variables in a function block -- depending on the actual
implementation.




More information about the Python-list mailing list