[Python-3000] Format specifier proposal

Andrew James Wade andrew.j.wade at gmail.com
Tue Aug 14 08:28:05 CEST 2007


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.

-- Andrew


More information about the Python-3000 mailing list