Python3: hex() on arbitrary classes

Philipp Hagemeister phihag at phihag.de
Tue Sep 1 11:22:31 EDT 2009


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 doesn't
python convert the object to int before constructing the hex string?

Regards,

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: <http://mail.python.org/pipermail/python-list/attachments/20090901/09416cd6/attachment.sig>


More information about the Python-list mailing list