[Python-3000] Please don't kill the % operator...
Talin
talin at acm.org
Thu Aug 16 17:49:28 CEST 2007
Guido van Rossum wrote:
> While we're not copying .NET *exactly*, most of the basic ideas are
> very similar; the discussion at this point is mostly about the
> type-specific mini-languages. My proposal is to use *exactly the same
> mini-language as used in 2.x %-formatting* (but without the '%'
> character), in particular: {0:10.3f} will format a float in a field of
> 10 characters wide with 3 digits behind the decimal point, and {0:08x}
> will format an int in hex with 0-padding in a field 8 characters wide.
> For strings, you can write {0:10.20s} to specify a min width of 10 and
> a max width of 20 (I betcha you didn't even know you could do this
> with %10.20s :-). The only added wrinkle is that you can also write
> {0!r} to *force* using repr() on the value. This is similar to %r in
> 2.x. Of course, non-numeric types can define their own mini-language,
> but that's all advanced stuff. (The concept of type-specific
> mini-languages is straight from .NET though.)
Just to follow up on what Guido said:
The current language of the PEP uses a formatting mini-language which is
very close to the conversion specifiers of the existing '%' operator,
and which is backwards compatible with it. So essentially if you
understand printf-style formatting, you can use what you know.
It may be that additional formatting options can be added in a future
release of Python, however they should (a) be backwards compatible with
what we have now, (b) demonstrate a compelling need, and (c) require
their own PEP. In other words, I'm not going to add any more creeping
features to the current PEP. Except for updating the examples and adding
some clarifications that people have asked for, it's *done*.
I admit that part of this whole syntax discussion was my fault - I did
ask for a bit of a bikeshed discussion and I got way more than I
bargained for :)
-- Talin
More information about the Python-3000
mailing list