I know how to print “all” global variables using the following code
for k,v in pairs(_G) do
print("Global key", k, "value", v)
end
So my question is how to do that for all variables that are accessible from the currently executing function, something that can do what locals() does for Python.
Here is an implementation of a
locals()function. It will return a table of locals from the calling scope:Notice that in the lua REPL, each line is a separate chunk with separate locals. Also, internal variables are returned (names start with ‘(‘ if you want to remove them):
Thanks for the accept. You have unlocked the last piece of the puzzle! 😉
Upvalues are local variables from outer scopes, that are used in the current function. They are neither in
_Gnor inlocals()Example (notice you have to use a for it to show up):