开发者

Backport of builtin function bin() for python 2.4

I wrote a program that uses builtin function bin(), but this function is new in Python version 2.6 开发者_JS百科and I would like to run this application also in Python versions 2.4 and 2.5.

Is there some backport of bin() for 2.4?


You can try this version (credit goes to the original author):

def bin(x):
    """
    bin(number) -> string

    Stringifies an int or long in base 2.
    """
    if x < 0: 
        return '-' + bin(-x)
    out = []
    if x == 0: 
        out.append('0')
    while x > 0:
        out.append('01'[x & 1])
        x >>= 1
        pass
    try: 
        return '0b' + ''.join(reversed(out))
    except NameError, ne2: 
        out.reverse()
    return '0b' + ''.join(out)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜