replace %(word) in a string

Peter Hansen peter at engcorp.com
Wed Sep 17 12:41:40 EDT 2003


Fred Pacquier wrote:
> 
> Mirko Zeibig <mirko-lists at zeibig.net> said :
> 
> > Especially useful together with the locals() function, which returns a
> > dictionary of locally defined variables:
> >
> > def foo():
> >     a = "Hello"
> >     b = "World"
> >     print "%(a)s %(b)s" % locals()
> 
> Yes, that's a wonderful feature. Recently though, I've wondered a couple of
> times : is there an easy way to substitute with both local AND global
> variables ?...

tempDict = locals()
tempDict.update(globals())

then just use tempDict for the dictionary...

You can also do it the other way around, of course, and you must decide
which you really want since the last-added dictionary will of course 
mask any keys which are also in the first-added dictionary.

-Peter




More information about the Python-list mailing list