Why use "locals()"

Carl Banks pavlovevidence at gmail.com
Tue Sep 15 18:29:53 EDT 2009


On Sep 15, 11:41 am, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
wrote:
> En Tue, 15 Sep 2009 11:18:35 -0300, Sion Arrowsmith  
> <s... at viridian.paintbox> escribió:
>
> > Sean DiZazzo  <half.ital... at gmail.com> wrote:
> > What I'm not clear about is under what circumstances locals() does
> > not produce the same result as vars() .
>
> py> help(vars)
> Help on built-in function vars in module __builtin__:
>
> vars(...)
>      vars([object]) -> dictionary
>
>      Without arguments, equivalent to locals().
>      With an argument, equivalent to object.__dict__.

Now *this* I would say is unPythonic: it's basically another obvious
way to do something with no additional benefits.

It'd be more useful (and less of a misnomer) if it would return an
object really repesenting the variable visibility at the current
scope, something like "globals().copy().update(locals())", while
taking care of edge cases like undeclared locals.


Carl Banks



More information about the Python-list mailing list