[Python-Dev] Re: PEP 292, Simpler String Substitutions

François Pinard pinard@iro.umontreal.ca
19 Jun 2002 22:20:49 -0400


[Barry A. Warsaw]

>     This PEP describes a simpler string substitution feature, also
>     known as string interpolation.  This PEP is "simpler" in two
>     respects:

>     1. Python's current string substitution feature (commonly known as
>        %-substitutions) is complicated and error prone.  This PEP is
>        simpler at the cost of less expressiveness.

>     2. PEP 215 proposed an alternative string interpolation feature,
>        introducing a new `$' string prefix.  PEP 292 is simpler than
>        this because it involves no syntax changes and has much simpler
>        rules for what substitutions can occur in the string.

For one, I do not like seeing `$' as a string prefix in Python, and wonder
if we could not merely go with `%' as we always did in Python.  At least,
it keeps a kind of clear cut distance between Python and Perl. :-)

>     In addition, the rules for what can follow a % sign are fairly
>     complex, while the usual application rarely needs such complexity.

This premise seems exaggerated to me.  `%' as it stands is not that
complex to understand.  Moreover, many of us use `%' formatting a lot,
so it is not so rare that the current `%' specification is useful.

>     1. $$ is an escape; it is replaced with a single $

Let's suppose we stick with `%', the above rule reduces to something
already known.

>     3. ${identifier} [...]

We could use %{identifier} as meaning `%(identifier)s'.  Clean.  Simple.

>     2. $identifier [...]

This is where the difficulty lies.  Since the PEP already suggests that
${identifier} was to be preferred over $identifier, why not just go a bit
forward, and drop 2. altogether?  Or else, how do you justify that using
it really make things more legible?

Then, the whole proposal would reduce to adding %{identifier}, and instead
of having `.sub()' methods or whatever, just stick with what we already have.

This would be a mild change instead of a whole new feature, and keep Python
a little more wrapped to itself.  Interpolation proposals I've seen always
looked a bit awkward and foreign so far.

I guess that merely adding %{identifier} would wholly satisfy the given
justifications for the PEP (that is, giving a mean for people to avoid
the %()s as error prone), with a minimal impact on the current Python
definition, and a bit less of a surprise.  Python does not have to look
like Perl to be useful, you know! :-)

> Handling Missing Keys

This would be a non-issue, by the fact that %(identifier)s behaviour,
for undefined identifier, is already what we want.

> The mapping argument is optional; if it is omitted then the mapping is
> taken from the locals and globals of the context in which the .sub()
> method is executed.

This is an interesting idea.  However, there are other contexts where the
concept of a compound dictionary of all globals and locals would be useful.
Maybe we could have some allvars() similar to globals() and locals(),
and use `... % allvars()' instead of `.sub()'?  So this would serve both
string interpolation and other avenues.

I hope I succeed to express my feeling that we should try keeping string
interpolation rather natural with what Python already is.  We should not
carelessly multiply paradigms.

-- 
François Pinard   http://www.iro.umontreal.ca/~pinard