Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Michael Torrie Newsgroups: comp.lang.python Subject: Re: Pyhon 2.x or 3.x, which is faster? Date: Tue, 8 Mar 2016 17:34:59 -0700 Lines: 23 Message-ID: References: <87d1r6iltx.fsf@elektro.pacujo.net> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de NOz7PoHEvEw7zP6Gk494DAUJ6qVGv0vjbSj1q9Moi+zQ== 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; 'value,': 0.03; 'string.': 0.04; 'binary': 0.05; 'bytes.': 0.07; 'indexing': 0.07; '*is*': 0.09; 'bytes)': 0.09; 'read()': 0.09; 'subject:which': 0.09; 'python': 0.10; "'data'": 0.16; '2016': 0.16; 'from:addr:torriem': 0.16; 'from:name:michael torrie': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'there?': 0.16; 'wrote:': 0.16; 'string': 0.17; 'byte': 0.18; 'string,': 0.18; 'file:': 0.22; 'am,': 0.23; 'code,': 0.23; 'bit': 0.23; 'header:In-Reply-To:1': 0.24; 'sort': 0.25; 'header:User-Agent:1': 0.26; 'chris': 0.26; 'equivalent': 0.27; 'mode.': 0.29; 'array': 0.29; 'code': 0.30; 'message- id:@gmail.com': 0.34; 'tue,': 0.34; 'file': 0.34; 'handle': 0.34; 'skip:d 20': 0.34; 'but': 0.36; 'there': 0.36; 'to:addr:python- list': 0.36; 'subject:?': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'easily': 0.39; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'still': 0.40; 'some': 0.40; 'default': 0.61; 'charset:windows-1252': 0.62; 'between': 0.65; 'mar': 0.65; 'differences': 0.66; '(is': 0.84 X-Virus-Scanned: amavisd-new at torriefamily.org User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.5.1 In-Reply-To: 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:104367 On 03/07/2016 05:45 PM, Chris Angelico wrote: > On Tue, Mar 8, 2016 at 11:22 AM, BartC wrote: >> >> (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: >> >> data=array.array('B',data) >> >> then it still works - Python 3. But not on Python 2. If I do .read on a >> binary file I get a byte string in Python 3, but a string in Python 2. That >> sort of mess. > > The default string in Py2 *is* a byte string. There are some interesting differences I found between a Python 2 string (composed of bytes) and a Python 3 byte string, such as what you'd get from calling read() on a file handle opened in binary mode. That is in Python 2, indexing a string returns a string of length 1. In Python 3.5, indexing a byte string returns a value, the equivalent of calling ord() on the single byte string. This makes it a bit difficult to make the code easily work between Python 2 and 3 and handle bytes. Any ideas there?