How to define a bytes literal in Python 2.x for porting to Python 3.x using 2to3?

Stefan Behnel stefan_ml at behnel.de
Sun Jan 2 07:11:56 EST 2011


Terry Reedy, 01.01.2011 23:47:
> 1. Code running in multiple versions has to be syntactically correct in
> every detail all versions in order to be compiled without error. However,
> version specific syntax *can* be used in modules that are conditionally
> imported and therefore conditionally compiled and executed.

This is something that might also be a suitable solution for the OP's 
problem. The format strings can be by externalised into an importable 
module, which can then be duplicated to use the 'b' bytes prefix for Python 3.

The obvious drawback is that this moves the strings out of the immediate 
sight of someone reading the sources, and that it requires the two string 
modules to be kept in sync. But at least for the synchronisation, a 
simplistic conversion tool run during installation could do the trick.

Stefan




More information about the Python-list mailing list