This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: None float format: incomplete documentation
Type: enhancement Stage: resolved
Components: Documentation Versions: Python 2.7
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, eric.smith, iritkatriel, lebigot, mark.dickinson
Priority: normal Keywords:

Created on 2014-04-10 12:19 by lebigot, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (6)
msg215871 - (view) Author: Eric O. LEBIGOT (lebigot) Date: 2014-04-10 12:19
The documentation for a None (empty) format for floats indicates that it is equivalent to the g format. This does not appear to be correct (http://stackoverflow.com/questions/16525924/precise-definition-of-float-string-formatting).

The Python 3.4 documentation (https://docs.python.org/3.4/library/string.html#format-specification-mini-language) seems to be much closer to what Python 2.7 does.

It would be useful to have a more correct documentation for the effect of a None format for floats in Python 2.7 (maybe by copying the Python 3.4 documentation if it applies).
msg215878 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2014-04-10 13:18
An empty format specifier can (and to my knowledge, does) match str(). So you get:

>>> format(1e10, '')
'10000000000.0'
>>> format(1e10, 'g')
'1e+10'
>>> str(1e10)
'10000000000.0'

The SO question is asking about an empty "presentation type", which is indeed similar to 'g' for floats.

I think this is all the same in 2.7 and 3.4, with the exception of how str() might operate on floats.
msg215880 - (view) Author: Eric O. LEBIGOT (lebigot) Date: 2014-04-10 13:30
These examples are good.

I am confused, though, about "The SO question is asking about an empty "presentation type", which is indeed similar to 'g' for floats.": the question is actually about why the '' format gives a result that differs from the 'g' format despite the Python 2.7 documentation saying that a "None" format type is "The same as 'g'.". Both the SO question and your examples show that the Python 2.7 documentation is incorrect, unless the other commenters and I were missing something…

Clarifying the documentation would in any case be useful, as the comments to the SO question show…
msg215887 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2014-04-10 15:04
The rule is:
- if the entire format specifier is the empty string (not None, but ''), then return str(value)
- else, look at the presentation type. if it is missing, set it to something like 'g'
- do the normal float formatting using the presentation type

The first of these has an empty string for the format specifier (so uses str(1e10), the rest do not:

>>> format(1e10, '')
'10000000000.0'
>>> format(1e10, ' ')
' 10000000000.0'
>>> format(1e10, ' g')
' 1e+10'
>>> format(1e10, ' e')
' 1.000000e+10'
>>> format(1e10, ' f')
' 10000000000.000000'

Now, how much the "something like g" part of the above is true is debatable.
msg215913 - (view) Author: Eric O. LEBIGOT (lebigot) Date: 2014-04-11 02:41
The Python 2.7 goes even as far as to say that format(1e10, ' ') should give "the same as" format(1e10, ' g') (not something "similar to g"), which is obviously incorrect.

If the Python 3.4 documentation for the empty presentation type of floats were used in the Python 2.7 documentation, it would be closer to the real behavior of Python 2.7, so that would be an improvement.

Now, the real question is whether the Python 3.4 documentation applies exactly to Python 2.7, here, or whether improving the Python 2.7 documentation would require a different description.
msg382226 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2020-12-01 09:56
Python 2.7-only issue.
History
Date User Action Args
2022-04-11 14:58:01adminsetgithub: 65394
2020-12-01 09:56:48iritkatrielsetstatus: open -> closed

nosy: + iritkatriel
messages: + msg382226

resolution: out of date
stage: resolved
2014-04-11 02:41:28lebigotsetmessages: + msg215913
2014-04-10 15:04:53eric.smithsetmessages: + msg215887
2014-04-10 14:47:17mark.dickinsonsetnosy: + mark.dickinson
2014-04-10 13:30:26lebigotsetmessages: + msg215880
2014-04-10 13:18:48eric.smithsetnosy: + eric.smith
messages: + msg215878
2014-04-10 12:19:35lebigotcreate