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

 ̄綄美尐妖づ 提交于 2019-12-09 04:39:21

问题


In Python 2.6 (and earlier) the hex() and oct() built-in functions can be overloaded in a class by defining __hex__ and __oct__ special functions. However there is not a __bin__ special function for overloading the behaviour of Python 2.6's new bin() built-in function.

I want to know if there is any way of flexibly overloading bin(), and if not I was wondering why the inconsistent interface?

I do know that the __index__ special function can be used, but this isn't flexible as it can only return an integer. My particular use case is from the bitstring module, where leading zero bits are considered significant:

>>> a = BitString(length=12)       # Twelve zero bits
>>> hex(a)
'0x000'
>>> oct(a)
'0o0000'
>>> bin(a)
'0b0' <------ I want it to output '0b000000000000'

I suspect that there's no way of achieving this, but I thought it wouldn't hurt to ask!


回答1:


As you've already discovered, you can't override bin(), but it doesn't sound like you need to do that. You just want a 0-padded binary value. Unfortunately in python 2.5 and previous, you couldn't use "%b" to indicate binary, so you can't use the "%" string formatting operator to achieve the result you want.

Luckily python 2.6 does offer what you want, in the form of the new str.format() method. I believe that this particular bit of line-noise is what you're looking for:

>>> '{0:010b}'.format(19)
'0000010011'

The syntax for this mini-language is under "format specification mini-language" in the docs. To save you some time, I'll explain the string that I'm using:

  1. parameter zero (i.e. 19) should be formatted, using
  2. a magic "0" to indicate that I want 0-padded, right-aligned number, with
  3. 10 digits of precision, in
  4. binary format.

You can use this syntax to achieve a variety of creative versions of alignment and padding.




回答2:


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.




回答3:


The bin function receives it's value from the object's __index__ function. So for an object, you can define the value converted to binary, but you can't define the format of the string.




回答4:


You could achieve the same behaviour as for hex and oct by overriding/replacing the built in bin() function with your own implementation that attempted to call bin on the object being passed and fell back to the standard bin() function if the object didn't provide bin. However, on the basis that explicit is better than implicit, coding your application to depend on a custom version of bin() is probably not a good idea so maybe just give the function a different name e.g.

def mybin(n):
    try:
        return n.__bin__()
    except AttributeError:
        return bin(n)

As for why the inconsistency in the interface, I'm not sure. Maybe it's because bin() was added more recently so it's a slight oversight?



来源:https://stackoverflow.com/questions/1002116/can-bin-be-overloaded-like-oct-and-hex-in-python-2-6

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