What is the difference between globals(), locals(), and vars()? What do they return? Are updates to the results useful?
What is the difference between globals() , locals() , and vars() ? What do
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Each of these return a dictionary:
globals()always returns the dictionary of the module namespacelocals()always returns a dictionary of the current namespacevars()returns either a dictionary of the current namespace (if called with no argument) or the dictionary of the argument.localsandvarscould use some more explanation. Iflocals()is called inside a function, it updates a dict with the values of the current local variable namespace (plus any closure variables) as of that moment and returns it. Multiple calls tolocals()in the same stack frame return the same dict each time – it’s attached to the stack frame object as itsf_localsattribute. The dict’s contents are updated on eachlocals()call and eachf_localsattribute access, but only on such calls or attribute accesses. It does not automatically update when variables are assigned, and assigning entries in the dict will not assign the corresponding local variables:gives us:
The first
print(l)only shows an'x'entry, because the assignment tolhappens after thelocals()call. The secondprint(l), after callinglocals()again, shows anlentry, even though we didn’t save the return value. The third and fourthprints show that assigning variables doesn’t updateland vice versa, but after we accessf_locals, local variables are copied intolocals()again.Two notes:
exec "pass"line in the function. This switches the function to an older, slower execution mode that uses thelocals()dict as the canonical representation of local variables.If
locals()is called outside a function it returns the actual dictionary that is the current namespace. Further changes to the namespace are reflected in the dictionary, and changes to the dictionary are reflected in the namespace:gives us:
So far, everything I’ve said about
locals()is also true forvars()… here’s the difference:vars()accepts a single object as its argument, and if you give it an object it returns the__dict__of that object. For a typical object, its__dict__is where most of its attribute data is stored. This includes class variables and module globals:which gives us:
Note that a function’s
__dict__is its attribute namespace, not local variables. It wouldn’t make sense for a function’s__dict__to store local variables, since recursion and multithreading mean there can be multiple calls to a function at the same time, each with their own locals:which gives us:
Here,
fcalls itself recursively, so the inner and outer calls overlap. Each one sees its own local variables when it callslocals(), but both calls see the samef.__dict__, andf.__dict__doesn’t have any local variables in it.