[New-bugs-announce] [issue29329] Incorrect documentation for custom `hex()` support on Python 2

Pekka Klärck report at bugs.python.org
Thu Jan 19 13:55:38 EST 2017

New submission from Pekka Klärck:

Documentation of `hex()` on Python 2 says that custom objects need to implement `__index__` to support it. Based on my tests that doesn't work but `__hex__` is needed instead. Docs are at 
https://docs.python.org/2/library/functions.html?highlight=hex#hex and here's an example session:

Python 2.7.6 (default, Oct 26 2016, 20:30:19) 
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class Hex(object):
...     def __index__(self):
...         return 255
>>> hex(Hex())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: hex() argument can't be converted to hex
>>> class Hex(object):
...     def __hex__(self):
...         return hex(255)
>>> hex(Hex())

Assuming this is fixed, should probably note that with Python 3 you actually *need* to implement `__index__` and `__hex__` has no effect.

messages: 285832
nosy: pekka.klarck
priority: normal
severity: normal
status: open
title: Incorrect documentation for custom `hex()` support on Python 2

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list