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

python at bdurham.com python at bdurham.com
Tue Feb 10 23:10:35 EST 2009


Thank you Gabriel!

Malcolm


----- Original message -----
From: "Gabriel Genellina" <gagsl-py2 at yahoo.com.ar>
To: python-list at python.org
Date: Wed, 11 Feb 2009 02:04:47 -0200
Subject: Re: Difference between vars() and locals() and use case for
vars()

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

--
http://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list