string interpolation syntactic sugar

Dickon Reed dr10009 at cl.cam.ac.uk
Fri Dec 10 09:52:44 EST 1999


In article <14415.57793.278683.360085 at goon.cnri.reston.va.us>,
Jeremy Hylton  <jeremy at cnri.reston.va.us> wrote:
>When I have complicated formats, I usually turn to dictionaries rather
>than trying to match up format strings with a tuple of values:
>
>"a %(x)s  b %(y)s"  % { 'x': math.log(0),
>                        'y': y }
>
>I'm not sure that the suggested approach of putting some kind of
>special delimiters to mark eval-able parts of strings is much of an
>improvement.

I didn't know that about this aspect of %. The only difference is one
of readability. I think that:

interpolate("a {math.log(0)}  b {y}")

is more readable than your code. The readability becomes more
important the longer the string gets. It's a question really about
whether the decoupling in your approach is worse than the extra
knowledge required (ie understanding what interpolate or some
syntactic sugar to do the same thing does). It seems borderline to
me. 

Dickon






More information about the Python-list mailing list