Whatever happened to String Interpolation?

Fernando Pérez fperez528 at yahoo.com
Wed Nov 28 13:10:23 EST 2001


Skip Montanaro wrote:

> 
>     fp> I'm a big fan of Ka-Ping Yee's Itpl module (which allows
>     strings of fp> the form 'the value of x is $x', but I often find
>     myself not using fp> it so as not to add an extra dependency to
>     a program I want to keep fp> small.
> 
> I know it's a little more verbose, but what's wrong with
> 
>     'the value of x is %(x)s' % locals()
> 
> (or something similar?  As has been demonstrated recently on the
> list, you can easily create a dict-like object that allows you to
> interpolate complex expressions as well.
> 

Too much to type and visually parse, for one. I've seen the neat 
tricks to evaluate more complex things, but they're not standard nor 
obvious (so I bet you 95% of people just create temps with what they 
need right before the evaluation).

A more serious one: dynamic code generation. Just today I had to jump 
through some annoying hoops (like defining x="'%s'" and y='%' and use 
those in a string) to generate code that would include % characters 
but which would first have to be parsed by a %. I know this isn't 
exactly common, though.

I am a big defender of not extending things unless truly needed, and 
with time I've learned to do things the 'python way', without saying 
at every corner 'but language_x has this feature!'. But string 
handling is the one that *constantly* feels kludgy in an otherwise 
stunningly clean and elegant language.

I felt that pep215 had a clean argument, the extension is very 
orthogonal to the existing features, and can make a lot of code 
simply a lot cleaner. I feel those are strong arguments.

Well, I can keep dreaming...

Cheers,

f



More information about the Python-list mailing list