string interpolation for python

Chris Rebert clp2 at rebertia.com
Mon Apr 2 06:23:15 EDT 2012


On Mon, Apr 2, 2012 at 2:11 AM, Yingjie Lan <lanyjie at yahoo.com> wrote:
<snip>
> I believe non of the other three alternatives are as terse and readable.
> We've got template based, formatting with dict, formatting with tuple.
> They all require the coder extra effort:
>
> Both template based and dict-based formatting require writing the
> identifier three times:

False. Only once is required, though the technique to achieve it is kinda hacky.

>>>> name = 'Peter'
>>>> "Are you %(name)s"%{'name':name}

"Are you %(name)s" % locals() # or vars()

> If dynamic string is used:
>>>> "Are you $name$?"
>
> Template:
>>>> Template("Are you $name?").substitute(name=name)

Template("Are you $name?").substitute(locals()) # or vars()

> It is three to one in compactness, what a magic 3!
<snip>
> Why the Python community is so
> hostile to new things now?

It's more conservative than hostile. Here's some insight:
http://www.boredomandlaziness.org/2011/02/status-quo-wins-stalemate.html

Personally, in isolation, the only part of your proposal I find
/truly/ objectionable is the support for arbitrary expressions, since
it would tend towards encouraging suboptimal factoring. But we also
don't live in an ideal world, so the existence of the other 3 (2 of
them particularly relatively similar) alternatives is a legitimate
practical concern when evaluating your proposal. Python is
middle-aged; it's a blessing and a curse.

Cheers,
Chris



More information about the Python-list mailing list