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.

Title: Incorrect documentation for custom `hex()` support on Python 2
Type: Stage: resolved
Components: Documentation Versions: Python 2.7
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Mariatta Nosy List: Mariatta, ammar2, docs@python, eryksun, pekka.klarck, python-dev, zach.ware
Priority: normal Keywords: easy, patch

Created on 2017-01-19 18:55 by pekka.klarck, last changed 2022-04-11 14:58 by admin. This issue is now closed.

File name Uploaded Description Edit
hex_method.diff ammar2, 2017-01-31 16:15 review
Messages (7)
msg285832 - (view) Author: Pekka Klärck (pekka.klarck) Date: 2017-01-19 18:55
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 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.
msg285835 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2017-01-19 19:44
Python 3 uses __index__ for bin(), oct(), and hex(), but Python 2 only uses __index__ for bin() and otherwise uses __oct__ and __hex__. Antoine overlooked this when updating the 2.7 docs for hex() in issue 16665.
msg286540 - (view) Author: Ammar Askar (ammar2) * (Python committer) Date: 2017-01-31 16:15
Attached patch changes the py2.7 documentation to point out that the method name is __hex__ and its return type is a string.
msg286747 - (view) Author: Zachary Ware (zach.ware) * (Python committer) Date: 2017-02-02 06:22
msg286748 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2017-02-02 06:27
New changeset d7804789368a by Mariatta Wijaya in branch '2.7':
Issue #29329: Improve documentation for hex(). Patch by Ammar Askar
msg286749 - (view) Author: Mariatta (Mariatta) * (Python committer) Date: 2017-02-02 06:33
Thanks, Pekka, Eryk, Ammar, and Zachary :)
msg286750 - (view) Author: Roundup Robot (python-dev) (Python triager) Date:
New changeset d03fd5d041b6f514daf0aabc0c159d8727c33980 by Mariatta Wijaya in branch '2.7':
Issue #29329: Improve documentation for hex(). Patch by Ammar Askar
Date User Action Args
2022-04-11 14:58:42adminsetgithub: 73515
2017-02-02 07:00:19python-devsetmessages: + msg286750
2017-02-02 06:49:43Mariattasetresolution: fixed
stage: commit review -> resolved
2017-02-02 06:33:57Mariattasetstatus: open -> closed

messages: + msg286749
2017-02-02 06:27:17python-devsetnosy: + python-dev
messages: + msg286748
2017-02-02 06:22:28zach.waresetnosy: + Mariatta, zach.ware
messages: + msg286747

assignee: docs@python -> Mariatta
stage: needs patch -> commit review
2017-01-31 16:15:23ammar2setfiles: + hex_method.diff

nosy: + ammar2
messages: + msg286540

keywords: + patch
2017-01-19 19:46:59serhiy.storchakasetnosy: + docs@python
versions: + Python 2.7
assignee: docs@python
components: + Documentation
keywords: + easy
stage: needs patch
2017-01-19 19:44:51eryksunsetnosy: + eryksun
messages: + msg285835
2017-01-19 18:55:38pekka.klarckcreate