Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Chris Angelico Newsgroups: comp.lang.python Subject: Re: Pyhon 2.x or 3.x, which is faster? Date: Wed, 9 Mar 2016 11:38:13 +1100 Lines: 39 Message-ID: References: <87d1r6iltx.fsf@elektro.pacujo.net> <56DF6FB3.6080000@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de s+y2PT4r3h8O1paxGOuQCg3nfwY4qfFeJm238xCsVttA== 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; 'versions,': 0.05; 'bytes.': 0.07; 'indexing': 0.07; 'type,': 0.07; 'cc:addr:python-list': 0.09; '*is*': 0.09; 'bytes)': 0.09; 'read()': 0.09; 'subject:which': 0.09; 'python': 0.10; 'wed,': 0.15; "'data'": 0.16; '2016': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'py3': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'repr.': 0.16; 'there?': 0.16; 'wrote:': 0.16; 'string': 0.17; 'byte': 0.18; 'bytes': 0.18; 'string,': 0.18; '>>>': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'file:': 0.22; 'am,': 0.23; 'code,': 0.23; 'bit': 0.23; 'header:In-Reply-To:1': 0.24; 'sort': 0.25; 'chris': 0.26; 'equivalent': 0.27; 'message-id:@mail.gmail.com': 0.27; 'mode.': 0.29; 'array': 0.29; 'work.': 0.30; 'code': 0.30; 'probably': 0.31; "can't": 0.32; 'changed': 0.33; 'michael': 0.33; 'steven': 0.33; 'tue,': 0.34; 'file': 0.34; 'handle': 0.34; 'skip:d 20': 0.34; 'received:google.com': 0.35; 'but': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'subject:?': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'received:209.85.213': 0.37; 'difference': 0.38; 'received:209': 0.38; 'does': 0.39; 'easily': 0.39; 'where': 0.40; 'still': 0.40; 'some': 0.40; 'default': 0.61; 'between': 0.65; 'mar': 0.65; 'differences': 0.66; '(is': 0.84; 'chrisa': 0.84; 'flaw': 0.84; 'to:none': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc; bh=HCjKcxE+QyL8UMvuxNkYojZ7iD+4jGMQEoqMiTivjqw=; b=xDt1R2twavcQq7P+Md7kLUs/MZmXbtphXCKEMejGvERJy7ZKDs2sqEdTblI7cJgI6y E5L/Sn9puNH+FvhDnBr0st2mj4zMgHzErEZN0ZZrsZGFXfe+VbKu8rGYi56kMk6F+pEO SfAFmnvGNuhUfkH9CHSLxfHxLo8kcd+18NQ7CkyRq4v7jZf4Mk91nA57M/1pcxTF9Vno PK5tog5Kt098/V24aKtoaGB7LQ8shxFp8Hwub1sVQ7a5Zf9wF2ap1CsmUA1/uv8y6ubN 6L/BDbQu68S20pww6Ai0GVGcIJyBk4sFtzuTKCYhlG4taQEsqxxt7b4OdiwlXg5yfCbi QNhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc; bh=HCjKcxE+QyL8UMvuxNkYojZ7iD+4jGMQEoqMiTivjqw=; b=BAuDy/1mXQUzZsn7ShRG34PVu8Adn3YxTp7JC2DqxsgmEWSfrAdLA3WkKBIaMYrTvh aQgmNts5ZFxMJ3eIdf1Uec+ZA+gKzrIRpFgD1h2Fc3Joh7nuYV+Z+XGWoyKuoCeaC7tF jT5hn2WQ4va1lkvhvUHQ6h+fidq9D1LZveE33BBEESUamQy4yjaYJdmCKFgAzkoTTT5e cVtuxxcVWkvGebn9U3Pj+rpWJheyp2a08rhb/hwBVHIJWGmOudm2KIkL8T7R/yn7QLYY p/fptQsNHnMK9bYXcCw+wtXsVPJoF5aNYsy66GsK/ZbcJMvg3DmB4SpWEqsOFtqGEvOT 1OIA== X-Gm-Message-State: AD7BkJLMzX7ky1P1uA/0CqudjG6GmOZCxeUkJZf9a3gIcKxO74fNFIjJb++XDxXedbcLQQLO3av+woWV0Fp3Wg== X-Received: by 10.50.57.11 with SMTP id e11mr21660376igq.13.1457483893798; Tue, 08 Mar 2016 16:38:13 -0800 (PST) In-Reply-To: <56DF6FB3.6080000@gmail.com> 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:104369 On Wed, Mar 9, 2016 at 11:34 AM, Michael Torrie wrote: > 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? As Steven commented, this is probably a design flaw in the Py3 bytes type, but it can't be changed now. For perfect compatibility, just slice: >>> b"asdf"[2:3] # Py2 'd' >>> b"asdf"[2:3] # Py3 b'd' Works in both versions, with the only difference being the repr. It's a bit clunky, but it does work. ChrisA