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: unexpected return from float.__repr__() for inf, -inf, nan
Type: enhancement Stage: resolved
Components: Versions: Python 3.5
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: cvrebert, ethan.furman, jaebae17, mark.dickinson, serhiy.storchaka, terry.reedy
Priority: normal Keywords:

Created on 2014-11-26 19:47 by jaebae17, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (5)
msg231724 - (view) Author: (jaebae17) Date: 2014-11-26 19:47
The help page for the built-in repr() states ''' For many types, this function makes an attempt to return a string that would yield an object with the same value when passed to eval()...'''

This holds true for non-inf/nan values of float():
>>> eval(repr(float('0.0')))
0.0

But for inf, -inf, and nan it does not:
>>> eval(repr(float('inf')))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
NameError: name 'inf' is not defined

Expected return from repr(float('inf')) was "float('inf')", but perhaps 'inf' response has too much history at this point to be changed.
msg231755 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2014-11-27 12:01
Previously discussed here: http://bugs.python.org/issue1732212
msg231756 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2014-11-27 12:07
A couple of points against this change:

1. If repr(float('inf')) became "float('inf')", we'd lose the invariant that float(repr(x)) recovers x for all floats (including infinities and nans).

2. Since repr and str are currently identical for floats, this change would either end up modifying str too, or making str and repr different;  neither option is particularly palatable.  While I can see a case for the repr returning something eval-able, I think the str of an infinity should remain as "inf" or "-inf".

3. For consistency, you'd need to change the complex and Decimal outputs, too.

4. The current output of repr and str is directly parseable by most other languages.

So -1 from me.

(Changing type to 'enhancement'; we use 'behaviour' for bugs, and this isn't one. :-)
msg231757 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-11-27 12:19
Workaround:

>>> eval('inf', {'inf': float('inf')})
inf
msg231846 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2014-11-29 01:17
Enhancements are only possible in 3.5 or beyond.  I agree with Mark. There is no compelling reason to break code with this change.  Hence it should be rejected.

Float is an odd duck.  All ints and all non-recursive lists, for instance, have a literal representation, and repr used that.  No range objects, for instance, have a literal representation, so repr uses a evaluable 'range(start, stop[, step])'  All float objects except 3 have a literal representation.

Int and float are both unusual builtins in parsing a string input to produce a non-string object.  For int, eval(repr(i)) == int(repr(i)) == i for all ints i.  Similarly, eval(repr(f)) == float(repr(f)) == f for all float objects f *except* for the same 3 special objects.  For those 3, a choice was make and we should stick with it.
History
Date User Action Args
2022-04-11 14:58:10adminsetgithub: 67140
2014-11-29 01:18:01terry.reedysetstatus: open -> closed

versions: + Python 3.5, - Python 2.7
nosy: + terry.reedy

messages: + msg231846
resolution: rejected
stage: resolved
2014-11-28 18:12:51cvrebertsetnosy: + cvrebert
2014-11-27 12:19:09serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg231757
2014-11-27 12:07:26mark.dickinsonsettype: behavior -> enhancement
messages: + msg231756
2014-11-27 12:01:44mark.dickinsonsetnosy: + mark.dickinson
messages: + msg231755
2014-11-26 20:25:37ethan.furmansetnosy: + ethan.furman
2014-11-26 19:47:16jaebae17create