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

Baptiste Lepilleur baptiste.lepilleur at gmail.com
Sat Jan 1 06:34:37 EST 2011


2011年1月1日 Stefan Behnel <stefan_ml at behnel.de>
> Terry Reedy, 01.01.2011 11:08:
>> On 1/1/2011 4:08 AM, Baptiste Lepilleur wrote:
>>>> Is there a way to mark string literals so that 2to3 automatically
>>> prefixes them with 'b'? Is there a simpler trick?
>>>>>>> Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit
>> (Intel)] on win32
>> Type "copyright", "credits" or "license()" for more information.
>> >>> b=b'abc'
>> >>> b
>> 'abc'
>>>> The b prefix does nothing in 2.7. It was specifically added for this type
>> of porting problem.
>>>> More precisely, it was added in Python 2.6, so older Python versions will
> consider it a syntax error.
>> 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.
>
Thanks, I'll go that way. I guess it is an area where 3to2 would be
better...
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110101/339592ba/attachment-0001.html>


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /