[Python-Dev] Multiple dicts for string interpolation?

Jim Fulton jim@digicool.com
Fri, 28 Jan 2000 10:02:12 -0500


Skip Montanaro wrote:
> 
> Every once in awhile I want to perform string interpolation using more than
> one dictionary.  One way is to build a dictionary that's a union of multiple
> dictionaries:
> 
>     dict = {}
>     dict.update(d1)
>     dict.update(d2)
>     ...
>     s = format % dict
> 
> Another way is the MultiDict approach that Digital Creations (used to?) use
> in their DocumentTemplate module (I can't remember the exact usage any
> more):
> 
>     dict = MultiDict()
>     dict.append(d1)
>     dict.append(d2)

Actually, push (and pop). The namspaces are managed as a stack.

>     ...
>     s = format % dict
> 
> A MultiDict object maintains a list of the dicts it's been fed and searches
> them in order when __getitem__ is called.
> 
> I'd like to propose a third alternative.  How about if the string
> interpolation function accepted a tuple of dictionaries directly:
> 
>     s = format % (d1, d2)
> 
> It would only be used when named interpolation was expected.  I don't think
> there would be any conflict with current % operator semantics.

Yes. In the current semantics, you output the two dictionaries.

Try: 

  '%s %s' % ({'hello':'skip'},{})

Jim

--
Jim Fulton           mailto:jim@digicool.com   Python Powered!        
Technical Director   (888) 344-4332            http://www.python.org  
Digital Creations    http://www.digicool.com   http://www.zope.org    

Under US Code Title 47, Sec.227(b)(1)(C), Sec.227(a)(2)(B) This email
address may not be added to any commercial mail list with out my
permission.  Violation of my privacy with advertising or SPAM will
result in a suit for a MINIMUM of $500 damages/incident, $1500 for
repeats.