Re: Python3: hex() on arbitrary classes

2009-09-02 Thread Mark Dickinson
On Sep 1, 5:31 pm, Philipp Hagemeister 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__ metho

Re: Python3: hex() on arbitrary classes

2009-09-01 Thread Philipp Hagemeister
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 i

Re: Python3: hex() on arbitrary classes

2009-09-01 Thread Stephen Hansen
On Tue, Sep 1, 2009 at 8:22 AM, Philipp Hagemeister wrote: > class X(object): >def __int__(self): return 42 >def __hex__(self): return '2b' #sic > > hex(X()) > > > What would you expect? Python2 returns '2b', but python 3(74624) throws > TypeError: 'X' object cannot be interpreted as an in

Re: Python3: hex() on arbitrary classes

2009-09-01 Thread Mark Dickinson
On Sep 1, 4:22 pm, Philipp Hagemeister wrote: > class X(object): >     def __int__(self): return 42 >     def __hex__(self): return '2b' #sic > > hex(X()) > > What would you expect? Python2 returns '2b', but python 3(74624) throws > TypeError: 'X' object cannot be interpreted as an integer. Why do