Overriding __hex__ in python 3?

后端 未结 1 481
不知归路
不知归路 2021-01-16 03:14

I have the following class:

from __future__ import print_function

class Proxy(object):
    __slots__ = [\'_value\']

    def __init__(self, obj):
        se         


        
相关标签:
1条回答
  • 2021-01-16 03:43

    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.
    
    0 讨论(0)
提交回复
热议问题