Check if variable is defined

Ken Seehof kens at sightreader.com
Fri May 18 00:56:02 EDT 2001


---- Original Message ----- 
From: "Courageous" <jkraska1 at san.rr.com>
Newsgroups: comp.lang.python
To: <python-list at python.org>
Sent: Thursday, May 17, 2001 5:37 PM
Subject: Re: Check if variable is defined


> 
> >> I once wondered if vars(), locals() or globals() have a time cost to
> >> produce the dictionary in a usable format
> 
> I'm too lazy to go over to my Python source tree at the moment,
> but I believe that these already _are_ in Python dictionaries, right
> inside Python. IOW, they aren't "producing" anything, they are
> simply giving you a reference to an already-existing construct.
> 
> C//

Yup.  You really don't need to look at the sources.  In fact, at the
interpreter all three functions give a reference to the same dictionary.

>>> v1 = vars()
>>> v2 = vars()
>>> g1 = globals()
>>> g2 = globals()
>>> l1 = locals()
>>> l2 = locals()
>>> id(v1), id(v2)
(3145180, 3145180)
>>> id(g1), id(g2)
(3145180, 3145180)
>>> id(l1), id(l2)
(3145180, 3145180)

- Ken






More information about the Python-list mailing list