Overriding __hex__ in python 3?

一世执手 提交于 2019-12-19 12:28:22

问题


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: 'Proxy' object cannot be interpreted as an integer

what do I need to implement to make Proxy be interpreted as an integer?


回答1:


One of the goals of PEP3100 (Miscellaneous Python 3.0 Plans) was to:

[remove] __oct__, __hex__: use __index__ in oct() and hex() instead.

To make this work, you need to implement __index__, likely as:

def __index__(self):
    # or self._value if you know _value is an integer already
    return operator.index(self._value)

You can see the commit that changed this behavior here:

r55905 | georg.brandl | 2007-06-11 10:02:26 -0700 (Mon, 11 Jun 2007) | 5 lines

Remove __oct__ and __hex__ and use __index__ for converting
non-ints before formatting in a base.

Add a bin() builtin.


来源:https://stackoverflow.com/questions/59116578/overriding-hex-in-python-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!