[Python-3000] Format specifier proposal
Andrew James Wade
andrew.j.wade at gmail.com
Wed Aug 15 06:42:04 CEST 2007
On Tue, 14 Aug 2007 21:12:32 -0500
Ron Adam <rrr at ronadam.com> wrote:
>
>
> Greg Ewing wrote:
> > Guido van Rossum wrote:
> >> On 8/13/07, Andrew James Wade <andrew.j.wade at gmail.com> wrote:
> >>
> >>> On Mon, 13 Aug 2007 20:53:26 -0700
> >>> "Guido van Rossum" <guido at python.org> wrote:
> >
> >>>> I propose that their
> >>>> __format__ be defined like this:
> >>>>
> >>>> def __format__(self, spec):
> >>>> return self.strftime(spec)
> >>> You loose the ability to align the field then.
> >
> > This might be a use case for the chaining of format specs
> > that Ron mentioned. Suppose you could do
> >
> > "{{1:spec1}:spec2}".format(x)
> >
> > which would be equivalent to
> >
> > format(format(x, "spec1"), "spec2")
That would be a solution to my concerns, though that would have to be:
"{ {1:spec1}:spec2}"
>
>
> What I was thinking of was just a simple left to right evaluation order.
>
> "{0:spec1, spec2, ... }".format(x)
>
> I don't expect this will ever get very long.
The first __format__ will return a str, so chains longer than 2 don't
make a lot of sense. And the delimiter character should be allowed in
spec1; limiting the length of the chain to 2 allows that without escaping:
"{0:spec1-with-embedded-comma,}".format(x)
My scheme did the same sort of thing with spec1 and spec2 reversed.
Your order makes more intuitive sense; I chose my order because I
wanted the syntax to be a generalization of formatting strings.
Handling the chaining within the __format__ methods should be all of
two lines of boilerplate per method.
-- Andrew
More information about the Python-3000
mailing list