Python3: hex() on arbitrary classes

Philipp Hagemeister phihag at
Tue Sep 1 17:22:31 CEST 2009

class X(object):
    def __int__(self): return 42
    def __hex__(self): return '2b' #sic


What would you expect? Python2 returns '2b', but python 3(74624) throws
TypeError: 'X' object cannot be interpreted as an integer. Why doesn't
python convert the object to int before constructing the hex string?


Philipp Hagemeister

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 204 bytes
Desc: OpenPGP digital signature
URL: <>

More information about the Python-list mailing list