Python3: hex() on arbitrary classes

Mark Dickinson dickinsm at
Wed Sep 2 09:07:33 CEST 2009

On Sep 1, 5:31 pm, Philipp Hagemeister <phi... at> wrote:
> Mark Dickinson wrote:
> > (...) If you want to be
> > able to interpret instances of X as integers in the various Python
> > contexts that expect integers (e.g., hex(), but also things like list
> > indexing), you should implement the __index__ method:
> Thanks. Somehow forgot this magic method and deleted it by accident.

I wonder whether it would make sense for the Python 2.x hex
function to fall back to __index__ when __hex__ isn't defined.
With the current setup, it's not clear how to write 2.x code
that will also run (post 2to3 translation) properly on 3.x.


More information about the Python-list mailing list