Whatever happened to String Interpolation?

Oren Tirosh oren-py-l at hishome.net
Sun Dec 2 10:03:41 EST 2001


On Sat, Dec 01, 2001 at 07:03:19PM +2328, Fernando Pérez wrote:
> Exactly my point. I think when I mentioned my need of inserting % in 
> strange dynamic strings I inadvertedly set the discussion off-course. 
> That was just a side comment, and by no means the meat of my point. 
> My important point is that Ping's PEP allows a _clear_ way of saying 
> 
>         "x is $x, f(x) is $f(x)"

Will this do?

	i"x is `x`, f(x) is `f(x)`"

Take a look at http://www.tothink.com/python/interpp

The difference from Ka-Ping Yee's proposal runs much deeper than the use 
of backquotes instead of $.  The expressions between backquotes are real 
python expressions that get syntax checked at compile time and generate 
bytecode. 

You may want to write your string formats in one place and pass them as
arguments to be formatted in another context.  To do this you can simply
wrap your string in a lambda function:

  fmt = lambda: i"x is `x`, f(x) is `f(x)`"

When this function is called in a different context the magic of nested 
scopes will do the rest.  If you feel this is too much "black magic" you 
can also pass arguments to the lambda function.

	Oren Tirosh





More information about the Python-list mailing list