Re: accessing local and global dictionaries

Guido.van.Rossum@cwi.nl
Thu, 02 Mar 1995 16:10:52 +0100

Me:
> > At the moment there's no optimizer for globals accesses. However,
> > this may change. I suppose that use of the vars() function will have
> > to disable this optimizer...

Donald Beaudry:
> Considering how often I use the vars() function I would really hate to
> see current or future optimizations disabled due to its use. I almost
> always consider the dictionary returned by vars() as read-only. It is
> quite useful as the right hand side of the format operator. I would
> rather see a read-only restriction added to the returned dictionary
> instead of the removal of optimizations. How common is the use vars()
> to modify the environment?

You have a point there. At the moment, dictionaries don't have a
read-only flag (although I've considered adding one to make
dictionaries "hashable"), nor does the optimization of global
variables exist. If I had though of this, I might have suggested that
Harri defined the enum() function as returning a string that the
caller must pass to exec -- this is surely a sufficient sign that
things are done that affect the dictionary! E.g.

def enum(names, first):
words = string.split(names)
result = ""
for w in words:
result = result + "%s = %d\n" % (w, first)
first = first+1
return result

exec enum('a b c', 2)

On the other hand, exec and eval() do take a dictionary, and vars() is
about the only way to get the current module's dictionary...

--Guido van Rossum, CWI, Amsterdam <mailto:Guido.van.Rossum@cwi.nl>
<http://www.cwi.nl/cwi/people/Guido.van.Rossum.html>