Update locals() / resolved

Chris Liechti cliechti at gmx.net
Sat Apr 27 17:41:31 EDT 2002


[posted and mailed]

holger krekel <pyth at devel.trillke.net> wrote in
news:mailman.1019941968.7326.python-list at python.org: 

> 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({})):

use 'dict' here:

class dictlookup(dict):

(or UserDict for older pythons than 2.2)

>     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
> 
> 
> 



-- 
Chris <cliechti at gmx.net>




More information about the Python-list mailing list