Update locals() / resolved

holger krekel pyth at devel.trillke.net
Sat Apr 27 17:11:00 EDT 2002


On Sat, Apr 27, 2002 at 10:23:26PM +0200, holger krekel wrote:
> On Sat, Apr 27, 2002 at 03:06:08PM -0500, jepler at unpythonic.net wrote:
> > On Sat, Apr 27, 2002 at 09:57:25PM +0200, holger krekel wrote:
> > > Hello,
> > > 
> > > how can you dynamically update the locals() namebindings?
> > 
> > You don't.  See the library documentation:
> >     locals()
> > 	Return a dictionary representing the current local symbol table. Warning:
> > 	The contents of this dictionary should not be modified; changes may not
> > 	affect the values of local variables used by the interpreter.

ok. i found a nice workaround (if one dare call it that:-)

class dictlookup(type({})):
    def __getattr__(self, name):
        return self[name]

then i can do:

def func(...,somedict,...):
    m = dictlookup(somedict)
    ...
       m.key1 
    ...

which is reasonably short and additionally seems more robust.
I think it's generally quite nice for dictionaries containing 
'strings' as keys which you know in advance and want to access
without noise. 

    holger





More information about the Python-list mailing list