Check if variable is defined

Michael Hudson mwh at python.net
Thu May 17 18:13:21 EDT 2001


pinard at iro.umontreal.ca (=?iso-8859-1?q?Fran=E7ois?= Pinard) writes:

> I once wondered if vars(), locals() or globals() have a time cost to
> produce the dictionary in a usable format

At module (or at the interpreter prompt) level, they're all identical:

>>> locals() is globals()
1

and do just return the dictionary that contains the module variables.

In a function, locals & vars must do some more work, and so might be
slower than an exception - but doing this in function scope is just
vile, whereas at module level it is merely questionable.

Cheers,
M.

-- 
  > I'm a little confused.
  That's because you're Australian!  So all the blood flows to
  your head, away from the organ most normal guys think with.
                        -- Mark Hammond & Tim Peters, comp.lang.python



More information about the Python-list mailing list