string interpolation for python

Evan Driscoll driscoll at cs.wisc.edu
Mon Apr 2 16:36:04 EDT 2012


On 01/-10/-28163 01:59 PM, Yingjie Lan wrote:
> Because of the d"..." format, it won't
> affect old ways of doing things one bit.
> Allowing dynamic string wouldn't hurt
> a bit to anything that is already there.

Why don't you just write a function that does it? I think someone 
already suggested this...

   import dynamic_strings       # write this
   d = dynamic_strings.dynamic

   x = 5
   print(d("x=$x$"))

?

Sure, it's not *quite* as pretty as if you could just say d"x=$x$", and 
you might have to do some hacky uglyness in the implementation to get at 
the locals of the calling procedure, but it solves a bazillion problems, 
such as:

1. YOU can do it, instead of hoping it gets into the mainline
    interpreter

2. You can do it NOW, and it will work with "any" version of Python

3. You have the freedom to easily add "eval from *this dictionary* if
    you want, which solves Steven D'Aprano's objection that your
    suggestion is too weak.

4. Languages changes should be viewed suspiciously in general.

Evan



More information about the Python-list mailing list