[Python-Dev] Multiple dicts for string interpolation?
Skip Montanaro
skip@mojam.com (Skip Montanaro)
Wed, 26 Jan 2000 08:30:09 -0600 (CST)
Tim> Skip, is the long-windedness of
Tim> dict = MultiDict()
Tim> dict.append(d1)
Tim> dict.append(d2)
Tim> ...
Tim> s = format % dict
Tim> the part you didn't like about that? If so, how about changing the
Tim> constructor to
Tim> def __init__(self, *dicts):
Tim> ...
Tim> instead so you could use it as a one-liner
Tim> format % MultiDict(d1, d2, ...)
Tim> ? That's exactly the same as the tuple idea, except there's a nice
Tim> descriptive word in the middle of it <wink>.
The long-windedness was part of it. The performance hit of composing
dictionaries thousands of times to perform a single format operation was
also a consideration.
Okay, side excursion into the Zope source tree...
What I was calling MultiDict is actually MultiMapping (written in C, BTW).
As a side effect of my Zope install here, I even already have it in sys.path
(go figure!). And it turns out to work just as Tim surmised:
>>> d1 = {"a": 1}
>>> d2 = {"b": 2}
>>> d = MultiMapping.MultiMapping(d1, d2)
>>> d["b"]
2
>>> d["a"]
1
Dang! Turns out Jim Fulton has a time machine also.
I guess the next question is to extend Ken's comment about getting it into
the Python core. Would that be something possible for 1.6? I used a Python
version of MultiMapping in an ancient version of DocumentTemplate. I'm sure
the C version has been around for at least two or three years and would
appear pretty darn stable, since it seems to be at the core of a lot of
Zope's coolness.
Skip