Re: accessing local and global dictionaries

Harri Pasanen (pa@tekla.fi)
Thu, 2 Mar 1995 16:52:36 +0200

"Guido" == Guido van Rossum <Guido.van.Rossum@cwi.nl> writes:

Harri> But my actual question concerned deciphering the dictionaries for
Harri> different scopes. Any pointers there?

Guido> I was hoping I wouldn't have to answer that question, because it is
Guido> seriously "bad style" to define functions that modify their calling
Guido> environment directly (as opposed to returning a value that the caller
Guido> stores in a variable, or modifying an object, e.g. a dictionary,
Guido> passed to them as an argument).

Guido> Why don't you define your enum() function as taking a third argument,
Guido> a dictionary into which the new names are stored. You can then call
Guido> it (at the module level, at least) as:

Guido> enum('a,b,c', 2, vars())

Guido> The built-in vars() function returns the current dictionary. When
Guido> called at the module level, you can actually modify this dictionary to
Guido> your heart's content. (Inside functions, restrictions apply -- see
Guido> below.)

Aha, this will do. I was looking for vars() functionality in the
manuals, but in vain. Could you please add to documentation at exec(),
eval(), namespaces, dictionaries, etc. a cross reference to the vars()
function? I first looked into exec and eval docs, and those mentioned
the local and global dictionary arguments, but did not mention how to
find out what they were.

Thanks,

Harri