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.

Author pekka.klarck
Recipients pekka.klarck
Date 2017-01-19.18:55:38
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1484852138.82.0.311515909069.issue29329@psf.upfronthosting.co.za>
In-reply-to
Content
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())
'0xff'


 
Assuming this is fixed, should probably note that with Python 3 you actually *need* to implement `__index__` and `__hex__` has no effect.
History
Date User Action Args
2017-01-19 18:55:38pekka.klarcksetrecipients: + pekka.klarck
2017-01-19 18:55:38pekka.klarcksetmessageid: <1484852138.82.0.311515909069.issue29329@psf.upfronthosting.co.za>
2017-01-19 18:55:38pekka.klarcklinkissue29329 messages
2017-01-19 18:55:38pekka.klarckcreate