Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #31297 > unrolled thread

portable unicode literals

Started byUlrich Eckhardt <ulrich.eckhardt@dominolaser.com>
First post2012-10-15 15:05 +0200
Last post2012-10-15 20:11 +0300
Articles 6 — 6 participants

Back to article view | Back to comp.lang.python


Contents

  portable unicode literals Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-10-15 15:05 +0200
    Re: portable unicode literals Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-15 13:26 +0000
    Re: portable unicode literals Dave Angel <d@davea.name> - 2012-10-15 09:32 -0400
    Re: portable unicode literals Alex Strickland <sscc@mweb.co.za> - 2012-10-15 15:41 +0200
      Re: portable unicode literals Duncan Booth <duncan.booth@invalid.invalid> - 2012-10-16 08:44 +0000
    Re: portable unicode literals Serhiy Storchaka <storchaka@gmail.com> - 2012-10-15 20:11 +0300

#31297 — portable unicode literals

FromUlrich Eckhardt <ulrich.eckhardt@dominolaser.com>
Date2012-10-15 15:05 +0200
Subjectportable unicode literals
Message-ID<t8etk9-5h9.ln1@satorlaser.homedns.org>
Hi!

I need a little nudge in the right direction, as I'm misunderstanding 
something concerning string literals in Python 2 and 3. In Python 2.7, 
b'' and '' are byte strings, while u'' is a unicode literal. In Python 
3.2, b'' is a byte string and '' is a unicode literal, while u'' is a 
syntax error.

This actually came as a surprise to me, I assumed that using b'' I could 
portably create a byte string (which is true) and using u'' I could 
portably create a unicode string (which is not true). This feature would 
help porting code between both versions. While this is a state I can 
live with, I wonder what the rationale for this is.

!puzzled thanks

Uli

[toc] | [next] | [standalone]


#31298

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2012-10-15 13:26 +0000
Message-ID<507c0ee8$0$6574$c3e8da3$5496439d@news.astraweb.com>
In reply to#31297
On Mon, 15 Oct 2012 15:05:01 +0200, Ulrich Eckhardt wrote:

> Hi!
> 
> I need a little nudge in the right direction, as I'm misunderstanding
> something concerning string literals in Python 2 and 3. In Python 2.7,
> b'' and '' are byte strings, while u'' is a unicode literal. In Python
> 3.2, b'' is a byte string and '' is a unicode literal, while u'' is a
> syntax error.
> 
> This actually came as a surprise to me, I assumed that using b'' I could
> portably create a byte string (which is true) and using u'' I could
> portably create a unicode string (which is not true). This feature would
> help porting code between both versions. While this is a state I can
> live with, I wonder what the rationale for this is.

It was a mistake that is corrected in Python 3.3.

You can now use u'' to create Unicode literals in both 2.x and 3.3 or 
better. This is a feature only designed for porting code though: you 
shouldn't use u'' in new code not intended for 2.x.


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#31299

FromDave Angel <d@davea.name>
Date2012-10-15 09:32 -0400
Message-ID<mailman.2202.1350307975.27098.python-list@python.org>
In reply to#31297
On 10/15/2012 09:05 AM, Ulrich Eckhardt wrote:
> Hi!
>
> I need a little nudge in the right direction, as I'm misunderstanding
> something concerning string literals in Python 2 and 3. In Python 2.7,
> b'' and '' are byte strings, while u'' is a unicode literal. In Python
> 3.2, b'' is a byte string and '' is a unicode literal, while u'' is a
> syntax error.
>
> This actually came as a surprise to me, I assumed that using b'' I
> could portably create a byte string (which is true) and using u'' I
> could portably create a unicode string (which is not true). This
> feature would help porting code between both versions. While this is a
> state I can live with, I wonder what the rationale for this is.
>
> !puzzled thanks
>
> Uli

Python 3.3 added that syntax, for easier porting.  You can now use
u"xyz" for a unicode string in both 2.x and 3.3

-- 

DaveA

[toc] | [prev] | [next] | [standalone]


#31303

FromAlex Strickland <sscc@mweb.co.za>
Date2012-10-15 15:41 +0200
Message-ID<mailman.2204.1350308926.27098.python-list@python.org>
In reply to#31297
On 2012/10/15 03:05 PM, Ulrich Eckhardt wrote:

> This actually came as a surprise to me, I assumed that using b'' I could
> portably create a byte string (which is true) and using u'' I could
> portably create a unicode string (which is not true). This feature would
> help porting code between both versions. While this is a state I can
> live with, I wonder what the rationale for this is.
>
> !puzzled thanks

u'' is legal in 3.3 again.

-- 
Regards
Alex

[toc] | [prev] | [next] | [standalone]


#31367

FromDuncan Booth <duncan.booth@invalid.invalid>
Date2012-10-16 08:44 +0000
Message-ID<XnsA0EE620C017FFduncanbooth@127.0.0.1>
In reply to#31303
Alex Strickland <sscc@mweb.co.za> wrote:

> On 2012/10/15 03:05 PM, Ulrich Eckhardt wrote:
> 
>> This actually came as a surprise to me, I assumed that using b'' I could
>> portably create a byte string (which is true) and using u'' I could
>> portably create a unicode string (which is not true). This feature would
>> help porting code between both versions. While this is a state I can
>> live with, I wonder what the rationale for this is.
>>
>> !puzzled thanks
> 
> u'' is legal in 3.3 again.
> 
and if you want it to work in 3.1 and 3.2 there is the uprefix import hook: 
https://bitbucket.org/vinay.sajip/uprefix


-- 
Duncan Booth http://kupuguy.blogspot.com

[toc] | [prev] | [next] | [standalone]


#31322

FromSerhiy Storchaka <storchaka@gmail.com>
Date2012-10-15 20:11 +0300
Message-ID<mailman.2221.1350321135.27098.python-list@python.org>
In reply to#31297
On 15.10.12 16:05, Ulrich Eckhardt wrote:
> I need a little nudge in the right direction, as I'm misunderstanding
> something concerning string literals in Python 2 and 3. In Python 2.7,
> b'' and '' are byte strings, while u'' is a unicode literal. In Python
> 3.2, b'' is a byte string and '' is a unicode literal, while u'' is a
> syntax error.
>
> This actually came as a surprise to me, I assumed that using b'' I could
> portably create a byte string (which is true) and using u'' I could
> portably create a unicode string (which is not true). This feature would
> help porting code between both versions. While this is a state I can
> live with, I wonder what the rationale for this is.

   from __future__ import unicode_literals

And now you can portable use b'' for a byte string and '' for a unicode 
string. When you will drop Python 2 support then just remove import from 
__future__.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web