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())
'0xff'


 
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 <rep...@bugs.python.org>
<http://bugs.python.org/issue29329>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to