Mark Dickinson added the comment:

> I propose a change to use str() for float objects as well. This could be 
> ported back to 2.7 as well

That would be a behaviour change for 2.7, and an undesirable one. `str` loses 
precision in Python 2, so e.g., `json.loads(json.dumps(pi)) == pi` would no 
longer be true. (Indeed, the json library was changed to use `repr` rather than 
`str` for floats at some point in the past, for exactly this reason.)

I assume that dbus.Double is a subclass of float. Is that correct? If that's 
the case, I'm a bit confused: `float.__repr__` behaves identically to 
`float.__str__` in Python versions >= 3.2, so I don't see what your suggested 
change would achieve.

>>> class MyFloat(float):
...     def __repr__(self): return "MyFloat(something_or_other)"
...     def __str__(self): return "1729.0"
... 
>>> x = MyFloat(2.3)
>>> import json
>>> json.dumps(x)
'2.3'

----------
nosy: +mark.dickinson

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue27934>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to