[Python-Dev] format specification mini-language docs...

Eric Smith eric at trueblade.com
Sat Nov 22 03:03:43 CET 2008

Dino Viehland wrote:
> Ok, now I'm implementing __format__ support for IronPython.  The format spec mini-language docs say that a presentation type of None is the same as 'g' for floating point / decimal values.  

Awesome! Thanks for doing this.

 >But these two formats seem to differ based upon how they handle whole 
>>>> 2.0.__format__('')
> '2.0'
>>>> 2.0.__format__('g')
> '2'

I think the docs are wrong. For floats, the PEP 
(http://www.python.org/dev/peps/pep-3101/) says:

'' (None) - similar to 'g', except that it prints at least one digit 
after the decimal point.

> The docs also say that 'g' prints it as fixed point unless the number is too large.  But the fixed point format differs from what 'f' would print.  I guess it didn't say they'd both print it as fixed point w/ a precision of 6 or anything but it seems a little unclear.
>>>> 2.0.__format__('g')
> '2'
>>>> 2.0.__format__('f')
> '2.000000'

This is to be compatible with %-formatting:
$ ./python
Python 2.7a0 (trunk:67325, Nov 21 2008, 20:35:33)
[GCC 4.1.2 20070626 (Red Hat 4.1.2-13)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> '%f %g' % (2.0, 2.0)
'2.000000 2'

> Finally providing any sign character seems to cause +1.0#INF and friends to be returned instead of inf as is documented:
>>>> 10e667.__format__('+')
> '+1.0#INF'
>>>> 10e667.__format__('')
> 'inf'

Yes, that does seem odd. Let me look at it a bit and I'll comment on it, 
hopefully this weekend. I have a pending fix for 2.7/3.1 to make inf 
handling more consistent across platforms, it might take care of this 
case, too.

> Are these just doc bugs?  The inf issue is the only one that seems particularly weird to me.



More information about the Python-Dev mailing list