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

Martin v. Loewis martin at v.loewis.de
Sat Jan 1 17:07:15 EST 2011


>> To support older Python versions, you need to write your own wrapper
>> functions for bytes literals that do nothing in Python 2 and convert the
>> literal back to a bytes literal in Python 3. That's ugly, but there's no
>> other way to do it.
> 
> I think the developers expected that most maintained and updated 2.x
> code, especially code targeted at 3.x also, would be migrated to 2.6+.

Unfortunately, that assumption has hurt Python 3 migration
significantly. It gave the impression that, as long as you need to
support Python 2.5 and earlier, there is no way you could possibly
support Python 3 as well, and that, therefore, starting to support
Python 3 is pointless for many years to come.

I personally never shared that assumption, and encourage people to
ignore these gimmicks that had been added to 2.6 to ease porting.
Instead, people should first determine what Python versions their
users want to see supported, and then look for solutions that cover
all these versions.

In the case of byte literals, the solution is fairly straight-forward,
and only moderately ugly.

Regards,
Martin



More information about the Python-list mailing list