2
我編寫了一個使用內建函數bin()的程序,但是這個函數在Python 2.6中是新的,我也想在Python版本2.4和2.5中運行這個應用程序。python 2.4內置函數bin()的backport 2.4
是否有2.4的bin()的一些backport?
我編寫了一個使用內建函數bin()的程序,但是這個函數在Python 2.6中是新的,我也想在Python版本2.4和2.5中運行這個應用程序。python 2.4內置函數bin()的backport 2.4
是否有2.4的bin()的一些backport?
您可以嘗試this version(歸功於原作者):
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)