Concise idiom to initialize dictionaries

Frohnhofer, James james.frohnhofer at csfb.com
Tue Nov 9 13:59:09 EST 2004



exarkun at intarweb.us wrote:

>   This is actually not the problem being experienced.  The 
> actual problem is much more subtle and insidious.  Consider 
> this example:
> 
>     >>> locals()['x'] = 'y'
>     >>> print x
>     y
>     >>> 
> 
>   All appears well!  Mutating the object returned by locals() 
> lets us change arbitrary names in our local scope.  Great.  
> Now, let's go write a program:
> 
>     exarkun at boson:~$ cat > foo.py
>     def foo():
>         locals()['x'] = 'y'
>         print x
>     foo()
>     exarkun at boson:~$ python foo.py
>     Traceback (most recent call last):
>       File "foo.py", line 4, in ?
>         foo()
>       File "foo.py", line 3, in foo
>         print x
>     NameError: global name 'x' is not defined
>     exarkun at boson:~$ 
> 
>   "_Whaaaaat_?" you say.  "It's doing exactly the same 
> thing!" you say.  Well, not quite.

Exactly what happened.

> 
>   Mutating the objects returned by both locals() and 
> globals() is a no-no.  They are intended to provide _read 
> only_ access to those two scopes.  Since they return 
> dictionaries, you _can_ actually mutate them, though.  And 
> mutating globals() even works as expected; however, mutating 
> locals() does not.
> 
>   So why did it work in the interactive interpreter?  Well, 
> because of this nifty little fact:
> 
>     >>> locals() is globals()
>     True
>     >>> 
>

Ahhh. Now I see.
 
>   Oops.  At the top-level of the interactive interpreter, 
> there is no distinct local scope, just like at the top-level 
> of a module.  So the locals() function just returns the 
> globals dictionary, which happens to respect modifications.
> 
>   Kind of surprising, eh?
> 
>   Luckily for the original poster, namespaces aren't the only 
> dicts in town.  What he really wants to do is something like this:
> 

The number of dictionaries I need is 12.  I started out with a dictionary of
dictionaries, but my expressions (where I was actually using the dictionaries)
were pushed over the readibility edge by the extra level of indirection.  I
will probably stick with initializing them individually, preferring clear code
to compact code (while always trying for both).

Thanks to all who helped.

==============================================================================
This message is for the sole use of the intended recipient. If you received
this message in error please delete it and notify us. If this message was
misdirected, CSFB does not waive any confidentiality or privilege. CSFB
retains and monitors electronic communications sent through its network.
Instructions transmitted over this system are not binding on CSFB until they
are confirmed by us. Message transmission is not guaranteed to be secure.
==============================================================================




More information about the Python-list mailing list