Can bin() be overloaded like oct() and hex() in Python 2.6?

后端 未结 4 522
旧时难觅i
旧时难觅i 2021-02-05 12:26

In Python 2.6 (and earlier) the hex() and oct() built-in functions can be overloaded in a class by defining __hex__ and __oct__

4条回答
  •  無奈伤痛
    2021-02-05 13:10

    I think the short answer is 'No, bin() can't be overloaded like oct() and hex().'

    As to why, the answer must lie with Python 3.0, which uses __index__ to overload hex(), oct() and bin(), and has removed the __oct__ and __hex__ special functions altogether.

    So the Python 2.6 bin() looks very much like it's really a Python 3.0 feature that has been back-ported without much consideration that it's doing things the new Python 3 way rather than the old Python 2 way. I'd also guess that it's unlikely to get fixed, even if it is considered to be a bug.

提交回复
热议问题