Difference between vars() and locals() and use case for vars()

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Feb 10 23:04:47 EST 2009


En Wed, 11 Feb 2009 01:38:49 -0200, <python at bdurham.com> escribió:

> Can someone explain the difference between vars() and locals()?
> I'm also trying to figure out what the use case is for vars(),
> eg. when does it make sense to use vars() in a program?

Without arguments, vars() returns the current namespace -- same as  
locals() inside a function, same as locals() *and* globals() outside any  
function (i.e., in a module or running in the interactive interpreter)

With an argument (that is, vars(x)) it returns the names defined by the  
object itself; for "normal" class instances, that means its __dict__

It's useful in the interactive interpreter, to examine some object's  
contents. Maybe in other special cases. Certainly not in everyday's  
programming.

-- 
Gabriel Genellina




More information about the Python-list mailing list