New submission from Ethan Furman:

In Py3k __hex__ and __oct__ were removed, and hex() and oct() switched to use 
__index__.

hex() and oct() should be using __int__ instead.

Having read through PEP 357 [1] I see that __index__ is /primarily/ concerned 
with allowing arbitrary objects to be used in slices, but will also allow 
Python to convert an object to an int "whenever it needs one".

The problem is that my dbf [2] module has a couple custom types (Logical and 
Quantum) that allow for ternary logic (False/True/Unknown).  As much as 
possible Logical is intended to be a drop in replacement for the bool type, but 
of course there are some differences:

  - internally 'unknown' is represented as None

  - attempts to get an int, float, complex, etc., numeric value
    on Unknown raises an Exception

  - attempts to get the numeric string value via __hex__ and
    __oct__ on Unknown raises an Exception

  - when Unknown is used as an index (via __index__), 2 is returned

The problem, of course, is that in Python 3 calling hex() and oct() on Unknown 
now succeeds when it should be raising an exception, and would be if __int__ 
were used instead of __index__.

In summary, if Python just switches to using __index__, there would be little 
point in having separate __int__ and __index__ methods.


[1] http://www.python.org/dev/peps/pep-0357
[2] https://pypi.python.org/pypi/dbf

----------
messages: 206238
nosy: ethan.furman
priority: normal
severity: normal
status: open
title: hex() and oct() use __index__ instead of __int__
type: behavior
versions: Python 3.4

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue19988>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to