python 2.4的内置函数bin()的后端口
我编写了一个使用内置函数bin()的程序,但是这个函数在Python版本2.6中是新的,我想在Python版本2.4和2.5中运行这个应用程序 2.4版是否有bin()的后台端口?您可以尝试(归功于原作者):python 2.4的内置函数bin()的后端口,python,binary,backport,built-in,Python,Binary,Backport,Built In,我编写了一个使用内置函数bin()的程序,但是这个函数在Python版本2.6中是新的,我想在Python版本2.4和2.5中运行这个应用程序 2.4版是否有bin()的后台端口?您可以尝试(归功于原作者): def箱(x): """ 箱子(编号)->字符串 在基2中字符串化整型或长型。 """ 如果x0时: out.append('01'[x&1]) x>>=1 通过 尝试: 返回“0b”+“”。联接(反向(输出)) 除NameError外,ne2: out.reverse() 返回“0b”+
def箱(x):
"""
箱子(编号)->字符串
在基2中字符串化整型或长型。
"""
如果x<0:
返回'-'+bin(-x)
out=[]
如果x==0:
out.append('0')
当x>0时:
out.append('01'[x&1])
x>>=1
通过
尝试:
返回“0b”+“”。联接(反向(输出))
除NameError外,ne2:
out.reverse()
返回“0b”+“”。加入(out)
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)