Overriding __hex__ in python 3?
问题 I have the following class: from __future__ import print_function class Proxy(object): __slots__ = ['_value'] def __init__(self, obj): self._value = obj def __hex__(self): return hex(self._value) print(hex(42)) print(hex(Proxy(42))) in Python 2.7 this prints (py2.7) c:\hextest> python hextest.py 0x2a 0x2a but in Py3.8 this raises an exception: (py3.8) c:\hextest> python hextest.py 0x2a Traceback (most recent call last): File "hextest.py", line 14, in <module> print(hex(Proxy(42))) TypeError: