Python 2.4用のビルトイン関数bin()のバックポート
質問
私はBuiltin Function Bin()を使用するプログラムを書きましたが、この関数はPythonバージョン2.6では新しく、このアプリケーションもPythonバージョン2.4および2.5で実行したいと思います。
2.4のbin()のバックポートはありますか?
解決
あなたが試すことができます このバージョン (クレジットは元の著者に送られます):
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)
所属していません StackOverflow