[Python-3000] Please don't kill the % operator...

Barry Warsaw barry at python.org
Thu Aug 16 15:43:29 CEST 2007


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Aug 16, 2007, at 8:29 AM, skip at pobox.com wrote:

>     Alex> The PEP abstract says this proposal will replace the '%'  
> operator,
>
> I hope this really doesn't happen.  printf-style formatting has a long
> history both in C and Python and is well-understood.  Its few  
> limitations
> are mostly due to the binary nature of the % operator, not to the  
> power or
> flexibility of the format strings themselves.  In contrast, the new  
> format
> "language" seems to have no history (is it based on features in other
> languages?  does anyone know if it will actually be usable in common
> practice?) and at least to the casual observer of recent threads on  
> this
> topic seems extremely baroque.
>
> Python has a tradition of incorporating the best ideas from other  
> languages.
> String formatting is so common that it doesn't seem to me we should  
> need to
> invent a new, unproven mechanism to do this.

There are two parts to this, one is the language you use to define  
formatting and the other is the syntax you use to invoke it.  I've  
been mostly ignoring the PEP 3101 discussions because every time I  
see examples of an advanced format string I shudder and resign myself  
to never remembering how to use it.  It certainly doesn't feel like  
it's going to fit /my/ brain.

OTOH, I'm not saying that a super whizzy all-encompassing inscrutable- 
but-powerful format language is a bad thing for Python, but it may  
not be the best /default/ language for formatting.  OTOH, I don't  
think the three different formatting languages need three different  
syntaxes to invoke them.

The three formatting languages are, in order of decreasing simplicity  
and familiarity, but increasing power:

- - PEP 292 $-strings
- - Python 2 style % substitutions
- - PEP 3101 format specifiers

I think all three languages have their place, but I would like to see  
if there's some way to make spelling their use more consistent.  Not  
that I have any great ideas on that front, but I think the proposals  
in PEP 3101 (which I've only skimmed) are tied too closely to the  
latter format language.

While I agree with Skip's sentiment, I'd say that it's not the %- 
operator I care as much about as it is the middle road that the  
formatting language it uses takes.  For example, the logging package  
uses the same language but exposes a better (IMO) way to spell its use:

 >>> log.info('User %s ate %s', user, food)

So the question is, is there some way to unify the use of format  
strings in the three different formatting languages, giving equal  
weight to each under the acknowledgment that all three use cases are  
equally valid?

- -Barry

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (Darwin)

iQCVAwUBRsRUgnEjvBPtnXfVAQIgPgP9EeHyZRdco1w5yUG1ro8UoTMFJ5ppsxcK
Lyif38XaXTCL0t5nvxbvvI1GZksOHY4qyUwmUYrs+APhJbfSXfoGU1Ih+CzJhWPE
1PMng4s2z2pubpqGbAgV6etHx7Uiy8RPxp9lsD6rBo4GdtJwfTAFGgRgU67foMBl
ijlxMujdOR0=
=tw3Y
-----END PGP SIGNATURE-----


More information about the Python-3000 mailing list