string interpolation syntactic sugar
Dickon Reed
dr10009 at cl.cam.ac.uk
Fri Dec 10 09:31:16 EST 1999
In article <02d801bf4259$fa2737e0$f29b12c2 at secret.pythonware.com>,
Fredrik Lundh <fredrik at pythonware.com> wrote:
>Dickon Reed <dr10009 at cl.cam.ac.uk> wrote:
>> I propose a new kind of string literal wherein bracketed expressions
>> are evaluated then converted to strings.
>
>argh!
:-)
>> An alternative approach might have a function to do this:
>>
>> interpolate("a {x} b {y}")
>
>I'm not sure if it works with 1.5.2, but Ping's string
>interpolation module implements most of this
Ping's module is interesting. I rewrote it to the syntax I had in
mind, because I still prefer mine (being easier to describe and
implement). If anyone's interested, my interpolate module is at:
http://www.cl.cam.ac.uk/~dr10009/interpolate.py
Doing the thing I originally suggested in the interpreter is still
probably going to be more efficient, but this way is reasonably
backwards compatible.
The try: 1/0 except: munge traceback thing seems a little nasty to me;
anyone know of a way of getting at the caller's locals and globals
without throwing and catching an exception?
Dickon
ps. I wasn't aware of the use of % with dictionaries discussed in the
other branch of this thread; string % locals() idiom is nice, but I
hadn't seen it anywhere before.
More information about the Python-list
mailing list