Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Dennis Lee Bieber Newsgroups: comp.lang.python Subject: Re: Pyhon 2.x or 3.x, which is faster? Date: Mon, 07 Mar 2016 20:29:33 -0500 Organization: IISS Elusive Unicorn Lines: 48 Message-ID: References: <87d1r6iltx.fsf@elektro.pacujo.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de qS9ADuDKoxBJTw0sVD5Scgt0B4BXl+0ewSsvsIEMMoKQ== 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; 'binary': 0.05; 'assignment': 0.07; 'bytes.': 0.07; 'character,': 0.07; 'garbage': 0.09; 'message-id:@4ax.com': 0.09; 'mutable': 0.09; 'parsed': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:which': 0.09; 'typeerror:': 0.09; 'whole.': 0.09; 'python': 0.10; '"this': 0.13; 'explicitly': 0.15; "'data'": 0.16; "'this": 0.16; '(resulting': 0.16; '2016': 0.16; '>on': 0.16; 'binary.': 0.16; 'character).': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; "where's": 0.16; 'wrote:': 0.16; 'string': 0.17; 'byte': 0.18; 'bytes': 0.18; 'string,': 0.18; 'url:home': 0.18; '(in': 0.18; '>>>': 0.20; 'versions': 0.20; '"",': 0.22; 'ascii': 0.22; 'file:': 0.22; 'am,': 0.23; 'code,': 0.23; '(most': 0.24; 'header:X-Complaints-To:1': 0.26; 'chris': 0.26; 'object,': 0.27; 'sequence': 0.27; 'values': 0.28; 'array': 0.29; 'character': 0.29; 'read,': 0.29; "i'm": 0.30; 'traceback': 0.33; 'tue,': 0.34; 'file': 0.34; 'text': 0.35; 'unicode': 0.35; 'item': 0.35; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'charset:us-ascii': 0.37; 'end': 0.39; 'data': 0.39; 'does': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; "you'll": 0.61; 'real': 0.62; 'per': 0.62; 'mar': 0.65; 'dennis': 0.91 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: adsl-108-73-116-234.dsl.klmzmi.sbcglobal.net X-Newsreader: Forte Agent 6.00/32.1186 X-No-Archive: YES X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:104296 On Tue, 8 Mar 2016 00:22:06 +0000, BartC declaimed the following: >On 07/03/2016 23:40, Chris Angelico wrote: >> On Tue, Mar 8, 2016 at 9:39 AM, BartC wrote: > >>> I'm using it because this kind of file reading in Python is a mess. If I do >>> a read, will I get a string, a byte sequence object, a byte-array, or >>> array-array, or what? >> >> Uhh.... you'll get either a text string or a byte string, depending on >> whether you opened the file as text or binary. Where's the mess? > >(Is a byte string the same as a byte array? Is a byte array the same as >an array.array? If I remove this line from my code, where 'data' has >just been read from a file: In Python 3, text strings are Unicode (in current versions they will be 1, 2, or 4 bytes per character, depending on the "worst" character in the string... If everything is ASCII or ISO-Latin-1, a text string will be one byte per character). In Python 2, text strings are ALWAYS one byte per character (resulting in garbage if the real data is unicode). In Python 2, you have to explicitly ask that text be parsed as Unicode; conversely, Python 3 requires you to explicitly ask for binary bytes. In Python 2 you have to use bytearray() to create a "byte array". Byte array is mutable -- you can change values at each position without having to decompose and recompose the whole. Text strings are not mutable. >>> stng = "This is the end of the World" >>> ba = bytearray(stng) >>> ba bytearray(b'This is the end of the World') >>> stng[10] = "q" Traceback (most recent call last): File "", line 1, in TypeError: 'str' object does not support item assignment >>> ba[10] = "q" >>> stng 'This is the end of the World' >>> ba bytearray(b'This is thq end of the World') >>> -- Wulfraed Dennis Lee Bieber AF6VN wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/