Update locals()

holger krekel pyth at devel.trillke.net
Sat Apr 27 16:23:26 EDT 2002


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.

Uff. Thanks for the pointer although it is dissappointing. I always
liked to assume that python does this decision at run time. Doing
it at compile time may have performance benefits but it prevents
some nice uses. 

> Whenever a name is not bound inside a function, it is a global (it could
> never have a definition inside the function, so if the name comes from
> anywhere, it's global).  If there's a "global" declaration, then it's
> global.  If there's an assignment, then it's a local -- but changing a key
> in locals still doesn't work (or is at least permitted not to work).
> 
> You probably need to rethink the way you're approaching the problem.

maybe you have an advice?

Consider you have a dictionary e.g. returned from a
re.match - object. Is there no better way than

def func(somedict):

    ...
        somedict['keyname1']
    ...

to use the info? I have lots of descriptive
Named Patterns (via '?P<name>') and i want to work with
them in the function without redundancies. I really would like
an automatic pattern instead of doing

keyname1=somedict['keyname1']
keyname2=somedict['keyname2']
...

or something similar. I also have other uses but this one is the
easiest to explain.

    holger





More information about the Python-list mailing list