string interpolation for python

Yingjie Lan lanyjie at yahoo.com
Mon Apr 2 08:00:10 EDT 2012


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



This partly solves the problem, however, you 
can't work with expressions inside, like:

> d"sin($x$) = $sin(x)$"


Also, what if locals() or vars() does not contain
the variable "x"? (x could be nonlocal or global).


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

You are probably right...Harmless enhancement is still probable?


> 
> 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

I don't quite see that as a problem. The compiler (or translator, as you 
mentioned earlier) could easily make d"sin($x$) = $sin(x)$" into
something like: ''.join(["sin(", str(x), ") = ", str(sin(x))]
which would far more efficient than calling the format() method.

Cheers,
Yingjie



More information about the Python-list mailing list