[Python-Dev] Multiple dicts for string interpolation?
Tim Peters
tim_one@email.msn.com
Wed, 26 Jan 2000 00:39:19 -0500
[Skip, wants to interpolate multiple dicts via "%",
suggests passing a tuple of dicts: format % (d1, d2, ...)]
[Guido]
> ...
> I think it depends on to what extent this is a common, useful
> idiom. Do you have evidence of that? Examples?
You yourself raised one last century <wink>: simply wanting to interpolate
from both locals() and globals(). At the time, the idea of a new dict-like
mapping object (capturing Python's lookup rules) appealed to you. I still
like that, and note that the apparent need becomes more acute if "deep
nesting" is ever added.
I wasn't aware of the MultiDict approach Skip mentioned, but thought it
looked spot on for the general case! Skip, is the long-windedness of
dict = MultiDict()
dict.append(d1)
dict.append(d2)
...
s = format % dict
the part you didn't like about that? If so, how about changing the
constructor to
def __init__(self, *dicts):
...
instead so you could use it as a one-liner
format % MultiDict(d1, d2, ...)
? That's exactly the same as the tuple idea, except there's a nice
descriptive word in the middle of it <wink>.