[issue7028] Add int.hex for symmetry with float.hex
Mark Dickinson
report at bugs.python.org
Fri Oct 2 10:47:13 CEST 2009
Mark Dickinson <dickinsm at gmail.com> added the comment:
> Can int.hex() and int.fromhex() be added for symmetry?
On the face of it, adding int.hex (and presumably also long.hex for 2.x)
seems reasonable: in general, integers should be acceptable where-ever
floats are, and by that argument x.hex() should work regardless of
whether x is an integer or a float.
However, I'm opposed to adding int.hex, for a few reasons:
- the problem that float.hex solves is special to floats, namely that
the usual representation of a float (via repr) doesn't show the
*exact* value of that float clearly. This isn't a problem for
integers, Fractions, Decimals, etc.
- there's no danger of silent errors here: x.hex() will raise an
exception if x is an integer, giving the programmer an opportunity
to correct this to e.g. 'float(x).hex()', or perhaps
'x.hex() if isinstance(x, float) else hex(x)'. But which one?
Which leads me to:
- it's not clear what the output of n.hex() would be: e.g., should
(3).hex() match (3.0).hex(), or hex(3)? Or should it be something
else again? I'd suggest that different use-cases would need
different choices here.
- TSBO---APOO---OWTDI (see 'import this')
- I just don't see a real usecase for int.hex. float.hex is a
little-used but nice-to-have convenience function. When it's
used, it's almost certainly being used on a float. Feel free
to give examples of code that would benefit from int.hex.
The case for adding int.fromhex is even weaker: float.fromhex is a class
method, and will usually be called in the form
'float.fromhex(mystring)'. If you already know you want a hex_string to
int conversion, what's wrong with int(my_string, 16)?
In short, PBP (see 'import this' again).
If you really care about this, you could take the discussion to the
python-ideas mailing list and try to persuade people there; if everyone
feels that int.hex() *should* be added then I'll happily eat my words
and agree to add it.
Raymond, care to offer a second opinion?
----------
components: +Interpreter Core -Library (Lib)
nosy: +rhettinger
resolution: out of date ->
status: closed -> open
title: hex function should work with floats -> Add int.hex for symmetry with float.hex
versions: +Python 2.7, Python 3.2 -Python 3.1
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue7028>
_______________________________________
More information about the Python-bugs-list
mailing list