Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'context': 0.04; 'wed,': 0.04; '2.7': 0.05; 'string,': 0.05; '3.2': 0.07; 'ascii': 0.07; 'bytes.': 0.07; 'used.': 0.07; 'python': 0.07; '3.x': 0.09; 'bytes,': 0.09; 'default)': 0.09; 'default.': 0.09; 'encoding.': 0.09; 'internally': 0.09; 'messing': 0.09; 'utf-8': 0.09; 'pm,': 0.11; 'wrote:': 0.14; '(utf-16)': 0.16; '.py': 0.16; 'folks,': 0.16; 'installs': 0.16; 'mean,': 0.16; 'mythical': 0.16; 'output?': 0.16; 'range.': 0.16; 'subject:unicode': 0.16; 'ucs-4': 0.16; 'url:unicode': 0.16; 'what?': 0.16; 'compiled': 0.18; 'input': 0.18; 'anyway.': 0.19; 'bytes': 0.19; 'compile': 0.19; 'code': 0.22; 'header:In-Reply-To:1': 0.22; '\xa0if': 0.23; "didn't": 0.25; 'wrote': 0.25; 'byte': 0.25; 'specify': 0.25; 'stored': 0.25; 'used,': 0.25; 'installation': 0.25; "i'm": 0.26; 'message-id:@mail.gmail.com': 0.28; "doesn't": 0.28; 'knowing': 0.29; 'string': 0.29; 'problem': 0.29; 'assuming': 0.29; 'unicode': 0.29; 'books': 0.30; 'points': 0.31; '(just': 0.31; 'confused': 0.31; 'url:articles': 0.31; 'does': 0.31; 'called': 0.32; 'to:addr:python-list': 0.32; 'option': 0.33; 'worry': 0.33; 'things': 0.33; 'someone': 0.33; 'bit': 0.33; 'handling': 0.33; 'uses': 0.34; 'using': 0.34; 'change': 0.34; 'difference': 0.35; 'there': 0.35; 'file': 0.35; 'that,': 0.35; '2.6': 0.35; 'stuck': 0.35; 'rather': 0.36; 'think': 0.36; 'either': 0.37; '(by': 0.38; 'sequence': 0.38; 'received:google.com': 0.38; 'goes': 0.38; 'but': 0.38; 'so,': 0.38; 'used': 0.38; 'anything': 0.38; 'though': 0.38; 'affect': 0.39; 'signal': 0.39; 'sources': 0.39; 'set': 0.39; 'to:addr:python.org': 0.39; 'comes': 0.39; 'how': 0.39; '2011': 0.62; 'hands': 0.65; 'biggest': 0.71; 'capabilities': 0.72; 'article': 0.75; '11,': 0.77; 'correctly?': 0.84; 'of...': 0.84; 'received:129': 0.84; 'spoiled': 0.84; 'joel': 0.93 MIME-Version: 1.0 In-Reply-To: References: Date: Wed, 11 May 2011 15:34:02 -0700 Subject: Re: unicode by default From: Benjamin Kaplan To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Junkmail-Status: score=10/49, host=mpv1.tis.cwru.edu X-Junkmail-Signature-Raw: score=unknown, refid=str=0001.0A020207.4DCB0EE9.00C7,ss=1,fgs=0, ip=74.125.82.182, so=2010-12-23 16:51:53, dmn=2009-09-10 00:05:08, mode=single engine X-Junkmail-IWF: false X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 59 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1305153267 news.xs4all.nl 65870 [::ffff:82.94.164.166]:48459 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:5171 On Wed, May 11, 2011 at 2:37 PM, harrismh777 wrot= e: > hi folks, > =A0 I am puzzled by unicode generally, and within the context of python > specifically. For one thing, what do we mean that unicode is used in pyth= on > 3.x by default. (I know what default means, I mean, what changed?) > > =A0 I think part of my problem is that I'm spoiled (American, ascii herit= age) > and have been either stuck in ascii knowingly, or UTF-8 without knowing > (just because the code points lined up). I am confused by the implication= s > for using 3.x, because I am reading that there are significant things to = be > aware of... what? > > =A0 On my installation 2.6 =A0sys.maxunicode comes up with 1114111, and m= y 2.7 > and 3.2 installs come up with 65535 each. So, I am assuming that 2.6 was > compiled with UCS-4 (UTF-32) option for 4 byte unicode(?) and that the > default compile option for 2.7 & 3.2 (I didn't change anything) is set fo= r > UCS-2 (UTF-16) or 2 byte unicode(?). =A0 Do I understand this much correc= tly? > Not really sure about that, but it doesn't matter anyway. Because even though internally the string is stored as either a UCS-2 or a UCS-4 string, you never see that. You just see this string as a sequence of characters. If you want to turn it into a sequence of bytes, you have to use an encoding. > =A0 The books say that the .py sources are UTF-8 by default... and that 3= .x is > either UCS-2 or UCS-4. =A0If I use the file handling capabilities of Pyth= on in > 3.x (by default) what encoding will be used, and how will that affect the > output? > > =A0 If I do not specify any code points above ascii 0xFF does any of this > matter anyway? ASCII only goes up to 0x7F. If you were using UTF-8 bytestrings, then there is a difference for anything over that range. A byte string is a sequence of bytes. A unicode string is a sequence of these mythical abstractions called characters. So a unicode string u'\u00a0' will have a length of 1. Encode that to UTF-8 and you'll find it has a length of 2 (because UTF-8 uses 2 bytes to encode everything over 128- the top bit is used to signal that you need the next byte for this character) If you want the history behind the whole encoding mess, Joel Spolsky wrote a rather amusing article explaining how this all came about: http://www.joelonsoftware.com/articles/Unicode.html And the biggest reason to use Unicode is so that you don't have to worry about your program messing up because someone hands you input in a different encoding than you used.