Pregunta

I escribió un programa que utiliza incorporadas bin función (), pero esta función es nueva en Python versión 2.6 y me gustaría para ejecutar esta aplicación también en las versiones de Python 2.4 y 2.5.

¿Hay alguna backport de Bin () de 2.4?

¿Fue útil?

Solución

Puede probar esta versión (mérito es el autor original):

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)
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top