Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Stephen Hansen Newsgroups: comp.lang.python Subject: Re: Steve D'Aprano, you're the "master". What's wrong with this concatenation statement? Date: Tue, 10 May 2016 09:42:58 -0700 Lines: 68 Message-ID: References: <5730420b$0$1509$c3e8da3$5496439d@news.astraweb.com> <1462898578.1471033.603720241.686524F7@webmail.messagingengine.com> Mime-Version: 1.0 Content-Type: text/plain Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de A8Zcj3FmTvSAmRtACbJVRgjEf6/u5qtjipWwvhYwIOMw== 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; 'operator': 0.03; 'cpython': 0.05; 'dynamically': 0.07; 'linear': 0.07; 'subject:skip:c 10': 0.07; '"a"': 0.09; 'literal': 0.09; 'received:internal': 0.09; 'statements': 0.09; 'python': 0.10; 'instead.': 0.15; '(pypy,': 0.16; 'concat': 0.16; 'creator,': 0.16; 'dfs': 0.16; 'in-place': 0.16; 'jython,': 0.16; 'literal.': 0.16; 'literals': 0.16; 'message-id:@webmail.messagingengine.com': 0.16; 'places.': 0.16; 'received:10.202': 0.16; 'received:10.202.2': 0.16; 'received:10.202.2.44': 0.16; 'received:66.111': 0.16; 'received:66.111.4': 0.16; 'received:compute4.internal': 0.16; 'received:io': 0.16; 'received:messagingengine.com': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'string': 0.17; 'library,': 0.18; '(in': 0.18; 'language': 0.19; '>>>': 0.20; 'implicit': 0.22; 'occurs': 0.22; 'stephen': 0.22; 'am,': 0.23; "python's": 0.23; 'written': 0.24; 'header:In-Reply-To:1': 0.24; "doesn't": 0.26; '(it': 0.29; 'implicitly': 0.29; 'pep': 0.29; 'strings,': 0.29; 'that.': 0.30; 'code': 0.30; 'says': 0.32; 'statement': 0.32; 'steven': 0.33; 'recommended': 0.34; 'tue,': 0.34; 'except': 0.34; 'exist': 0.35; "isn't": 0.35; 'sometimes': 0.35; 'but': 0.36; 'should': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'two': 0.37; 'being': 0.37; 'no,': 0.38; 'received:66': 0.38; 'wrong': 0.38; 'building': 0.38; 'does': 0.39; 'subject:the': 0.39; 'to:addr:python.org': 0.40; 'subject:with': 0.40; 'some': 0.40; 'your': 0.60; "you'll": 0.61; 'header:Message-Id:1': 0.61; '(that': 0.63; 'note:': 0.66; 'guaranteed': 0.67; 'subject:, \n ': 0.84; 'subject:this': 0.85; 'subject:you': 0.85 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=ixokai.io; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-sasl-enc :x-sasl-enc; s=mesmtp; bh=MKoxnq4qWDRHWjO44G7h2B5af8E=; b=MVFwev KjKGurBkOLSUR0FVOGcr6qj21EJMuJwM8gQLaqaes+6KQyz7LHdSjNJTZgIgD4+3 CvkxZbgENZWvcsEdRGigmJCwx8sUl1cI4YEohMEoP0/MUm3njQHQ+y0ULYYrZR60 d0UJOnda3Mw0N6F9MMHb5IaPpJTmO6XfGEbIE= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-sasl-enc:x-sasl-enc; s=smtpout; bh=MKoxnq4qWDRHWjO 44G7h2B5af8E=; b=sCRm2Xjvjx+dHZmYFpu5ps4b5hDSBOd2ltw0EPutH+vinZI JUTeRY7N8S5X13uCqMffh+c1ruP3eRLnk7YdjXmMNNBQm/1DQ1hoJelRCG4GoMyW DuT7cPHIPZyBRESFNgHeqHASVDYkJnkApYDyxZaPnh2eEY1gns1bH03D0nlE= X-Sasl-Enc: SiswoMXjnRefbxDvAaj/Mivr8MHPCDayniC8KsQn/dMR 1462898578 X-Mailer: MessagingEngine.com Webmail Interface - ajax-de9b841d In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <1462898578.1471033.603720241.686524F7@webmail.messagingengine.com> X-Mailman-Original-References: <5730420b$0$1509$c3e8da3$5496439d@news.astraweb.com> Xref: csiph.com comp.lang.python:108474 On Tue, May 10, 2016, at 09:16 AM, DFS wrote: > But no, "master", your answers are incorrect. What's wrong with that > concat statement is that += concatenation is frowned upon by python's > creator, and is not recommended (in PEP8): > > > Code should be written in a way that does not disadvantage other > implementations of Python (PyPy, Jython, IronPython, Cython, Psyco, and > such). > > For example, do not rely on CPython's efficient implementation of > in-place string concatenation for statements in the form a += b or a = a > + b . This optimization is fragile even in CPython (it only works for > some types) and isn't present at all in implementations that don't use > refcounting. In performance sensitive parts of the library, the > ''.join() form should be used instead. This will ensure that > concatenation occurs in linear time across various implementations. > You once again misread PEP8. Not one of Steven's answers used string concatenation, except for the implicit literal concatenation, which all other implementations support. Note: > sSQL = """line 1 > line 2 > line 3""" No concatenation. One string literal. Works in all implementations. > sSQL = "line 1\nline 2\nline 3" No concatenation. One string literal. Works in all implementations. > > sSQL = ("line 1\n" > "line 2\n" > "line 3") Concatenation occurs at compile time, implicitly. Works in all implementations. The PEP says when building a string dynamically (that is, adding together two strings that exist and are separate), use "".join() in performance sensitive places. It says don't combine two strings with the addition operator -- because while that's sometimes efficient in CPython, the language doesn't guarantee it. You'll notice that the one time Steven combined two strings, it was implicitly at compile time. In every Python implementation, it is guaranteed that: >>> a = "a" "a" >>> b = "aa" Are the same. Two+ string literals are implicitly combined into one at compile time. > 'master craftswoman' my ass... Yes, you're being that. Please stop. -- Stephen Hansen m e @ i x o k a i . i o