Search code examples
pythonpython-3.xpython-2.5

How to define a binary string in Python in a way that works with both py2 and py3?


I am writing a module that is supposed to work in both Python 2 and 3 and I need to define a binary string.

Usually this would be something like data = b'abc' but this code code fails on Python 2.5 with invalid syntax.

How can I write the above code in a way that will work in all versions of Python 2.5+

Note: this has to be binary (it can contain any kind of characters, 0xFF), this is very important.


Solution

  • I would recommend the following:

    from six import b
    

    That requires the six module, of course. If you don't want that, here's another version:

    import sys
    if sys.version < '3':
        def b(x):
            return x
    else:
        import codecs
        def b(x):
            return codecs.latin_1_encode(x)[0]
    

    More info.

    These solutions (essentially the same) work, are clean, as fast as you are going to get, and can support all 256 byte values (which none of the other solutions here can).