[Python-Dev] PEP 292-related: why string substitution is not the same operation as data formatting

Michael McLay mclay@nist.gov
Fri, 12 Jul 2002 13:24:21 -0400


On Friday 12 July 2002 10:47 am, Guido van Rossum wrote:

> (FWIW, I agree with your other observations -- this was why I
> support exploring an alternative in PEP 292.)

The syntax rules of PEP 292 are likely to cause confusion for newbies who have 
never used sh or perl. They will ask why Python have two syntaxes for doing 
string substitutions? Why not always spell the substitution string with 
${identifier} or %(identifier)? The third rule of PEP292 in particular look 
like a patch to fix a kludge when an unanticipated exception was discovered.

   3. ${identifier} is equivalent to $identifier.  It is required for
          when valid identifier characters follow the placeholder but are
          not part of the placeholder, e.g. "${noun}ification".

> On Sunday 23 June 2002 02:16 pm, Lalo Martins wrote:
> > More, I'm completely opposed to "<<name>> is <<age:.0d>> years old"
> > because it's still cryptic and invasive. This should instead read similar 
> > to "<<name>> is <<age>> years old".sub({'name': x.name, 'age':
> > x.age.format(None, 0)})
>
> > Guido, can you please, for our enlightenment, tell us what are the
> > reasons you feel %(foo)s was a mistake?
>
> Because of the trailing 's'.  It's very easy to leave it out by
> mistake, and because the definition of printf formats skips over
> spaces (don't ask me why), the first character of the following word
> is used as the type indicator.

It's easy to leave it out by mistake, but the error is almost always 
immediately obvious. In the interest of keeping the language as simple as 
possible, I hope no changes are made. If a method based .sub() capability is 
to be added, why not reuse the %(identifier) syntax instead of introducing $ 
and ${} syntax? The .sub() string method would use the %(identifier) syntax 
without the 's' to spell the new substitution format. Instead of the 
proposed:

	'$name was born in ${country}'.sub()

the phrase would be spelled:

	'%(name) was born in %(country)'.sub()

This approach would introduce one new string method with a small variation on 
the existing '%' substitution syntax.