[Python-3000] Format specifier proposal

Andrew James Wade andrew.j.wade at gmail.com
Thu Aug 16 09:28:06 CEST 2007


On Thu, 16 Aug 2007 13:18:12 +1200
Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:

> Andrew James Wade wrote:
> > {1:!renewal date: %Y-%m-%d} # no special meaning for ! here.
> 
> Yuck. Although it might happen to work due to reuse of
> strftime, I'd consider that bad style -- constant parts
> of the output string should be outside of the format
> specs, i.e.:
> 
>    "renewal date: {1:%Y-%m-%d}".format(my_date)

To be sure; it's just that I couldn't think of a better example. My
point is by putting spec1 last, the only things you need to escape
are { and }. (They can be escaped as {lb} and {rb} by passing the
right parameters.)

The alteratives I see are:
1. [:spec1[,spec2]]   - {1: %B %d, %Y} doesn't work as expected.
2. [!spec2][:spec1]]  - order reversed.
                      - meaning of spec2 is overloaded by !r, !s.[1]
3. [:spec2[,spec1]]   - order reversed.
                      - spec1-only syntax is too elaborate: {1:,%Y-%m-%d}
4. [,spec2][:spec1]  long discussion here:
http://mail.python.org/pipermail/python-3000/2007-August/009066.html
                      - order reversed problem is particularly bad,
                        because : looks like it should have low
                        precedence.
                      - meaning of spec2 is overloaded by ,r ,s.[1]
                      - On the positive side, this is similar to .NET
                        syntax.
5. { {1:spec1}:spec2} - looks like a replacement field for the name
                        specifier. (Though a spec1 like %Y-%m-%d would
                        tend to counteract that impression.)

[1] This is particularly awkward since spec2 should be applied after
spec1, but !s and !r should be applied before spec1.

And in Talin's proposal, spec2 will be superfluous for strings and
integers. It's also not needed when all you want to do is align str(x).

I don't think any of them will fly :-(. My guess is that __format__
methods will do the chaining themselves with little standardization
on the syntax to do so.

-- Andrew


More information about the Python-3000 mailing list