Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'python.': 0.02; 'encoding': 0.05; 'string.': 0.05; 'string': 0.09; '32-bit': 0.09; 'dan': 0.09; 'integers': 0.09; 'pep': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'terms,': 0.09; 'subject:How': 0.10; 'python': 0.11; 'stored': 0.12; '(code': 0.16; '16-bit': 0.16; '3.3,': 0.16; 'integers.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:unicode': 0.16; 'url:peps': 0.16; 'wrote:': 0.18; 'thanks.': 0.20; 'header:User- Agent:1': 0.23; 'bytes': 0.24; 'received:comcast.net': 0.24; "shouldn't": 0.24; 'unicode': 0.24; 'url:dev': 0.24; "i've": 0.25; 'options': 0.25; 'header:X-Complaints-To:1': 0.27; 'header:In- Reply-To:1': 0.27; 'points': 0.29; 'characters': 0.30; 'specified': 0.30; 'code': 0.31; 'so-called': 0.31; 'probably': 0.32; 'url:python': 0.33; 'subject:the': 0.34; 'common': 0.35; 'but': 0.35; 'there': 0.35; 'sequence': 0.36; 'subject:?': 0.36; 'url:org': 0.36; 'sometimes': 0.38; 'depends': 0.38; 'to:addr :python-list': 0.38; 'pm,': 0.38; 'heard': 0.39; 'realize': 0.39; 'to:addr:python.org': 0.39; 'changed': 0.39; 'either': 0.39; 'received:org': 0.40; 'how': 0.40; 'tell': 0.60; 'more': 0.64; 'here': 0.66; 'details,': 0.68; 'examining': 0.84; 'internally.': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Ned Batchelder Subject: Re: How is unicode implemented behind the scenes? Date: Sat, 08 Mar 2014 22:48:51 -0500 References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: c-50-133-228-126.hsd1.ma.comcast.net User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Thunderbird/24.3.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 34 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1394336953 news.xs4all.nl 2877 [2001:888:2000:d::a6]:48495 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:68069 On 3/8/14 9:08 PM, Dan Stromberg wrote: > OK, I know that Unicode data is stored in an encoding on disk. > > But how is it stored in RAM? > > I realize I shouldn't write code that depends on any relevant > implementation details, but knowing some of the more common > implementation options would probably help build an intuition for > what's going on internally. > > I've heard that characters are no longer all c bytes wide internally, > so is it sometimes utf-8? > > Thanks. > In abstract terms, a Unicode string is a sequence of integers (code points). There are lots of ways to store a sequence of integers. In Python 2.x, it's either a vector of 16-bit ints, or 32-bit ints. These are the Unicode representations known as UTF-16 and UTF-32, respectively, and which you have depends on whether you have a "narrow" or "wide" build of Python. You can tell the difference by examining sys.maxunicode, which is 65535 (narrow) or 1114111 (wide). In Python 3.3, the representation was changed from narrow/wide to the so-called Flexible String Representation which others here have described. It uses either 1-, 2-, or 4-bytes per code point, depending on the set of code points in the string. It's specified in PEP 393: http://legacy.python.org/dev/peps/pep-0393/ -- Ned Batchelder, http://nedbatchelder.com