[Python-3000] Format specifier proposal

Guido van Rossum guido at python.org
Tue Aug 14 18:41:48 CEST 2007


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:
>
> ...
>
> > One of my favorite examples of non-numeric types are the date, time
> > and datetime types from the datetime module; here 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. What about:
>
>     def __format__(self, align_spec, spec="%Y-%m-%d %H:%M:%S"):
>         return format(self.strftime(spec), align_spec)
>
> with
>
>     def format(value, spec):
>         if "," in spec:
>             align_spec, custom_spec = spec.split(",",1)
>             return value.__format__(align_spec, custom_spec)
>         else:
>             return value.__format__(spec)
>
> ":,%Y-%m-%d" may be slightly more gross than ":%Y-%m-%d", but on the plus
> side ":30" would mean the same thing across all types.

Sorry, I really don't like imposing *any* syntactic constraints on the
spec apart from !r and !s.

You can get the default format with a custom size by using !s:30.

If you want a custom format *and* padding, just add extra spaces to the spec.

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-3000 mailing list