Path: csiph.com!usenet.pasdenom.info!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'cpython': 0.05; 'attribute': 0.07; 'subject: + ': 0.07; 'string': 0.09; 'items)': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'def': 0.12; "'to": 0.16; 'adjacent': 0.16; 'concat': 0.16; 'prev': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 's[1:]': 0.16; 'wrote:': 0.18; 'meant': 0.20; '>>>': 0.22; 'import': 0.22; 'preferred': 0.22; 'this?': 0.23; 'header:User-Agent:1': 0.23; 'helper': 0.24; '(for': 0.26; 'skip:" 20': 0.27; 'header:X-Complaints-To:1': 0.27; 'correct': 0.29; 'getting': 0.31; 'lines': 0.31; 'probably': 0.32; "i'd": 0.34; 'could': 0.34; 'subject:with': 0.35; 'operations': 0.35; 'but': 0.35; 'yield': 0.36; 'wrong': 0.37; 'so,': 0.37; 'to:addr:python-list': 0.38; 'bad': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'no.': 0.61; 'simple': 0.61; 'more': 0.64; 'determine': 0.67; 'obvious': 0.74; 'characters,': 0.84; 'subject:Using': 0.84; 'yours': 0.88; 'fibonacci': 0.91; 'inefficient': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Using + with strings considered bad Date: Wed, 29 Apr 2015 11:24:51 +0200 Organization: None References: <878udbxrpg.fsf@Equus.decebal.nl> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bd8d27.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ 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: 67 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1430299516 news.xs4all.nl 2936 [2001:888:2000:d::a6]:51093 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:89536 Cecil Westerhof wrote: > Because I try to keep my lines (well) below 80 characters, I use the > following: > print('Calculating fibonacci and fibonacci_memoize once for ' + > str(large_fibonacci) + ' to determine speed increase') > > But I was told that using + with strings was bad practice. Is this > true? No. What was meant was probably that str.join() is preferred when you are to concat an arbitrary number of strings, i. e. # wrong s = "" for item in items: s += " " + item.name # may be inefficient depending on implementation s = s[1:] # correct s = " ".join(item.name for item in items) (For more complex operations than just getting an attribute you may have to write a helper generator: def bogus(items): prev = "" for item in items: yield str(len(prev) - len(item)) prev = item s = "*".join(bogus(items)) ) > If so, what is the better way to do this? Python concats adjacent string constants implicitly >>> "one" "two" 'onetwo' but in CPython an extra + will be removed by the peephole optimiser: >>> def f(): return "one" + "two" ... >>> import dis >>> dis.dis(f) 1 0 LOAD_CONST 3 ('onetwo') 3 RETURN_VALUE > print('Calculating fibonacci and fibonacci_memoize once for ' + > str(large_fibonacci) + ' to determine speed increase') You could write that as print('Calculating fibonacci and fibonacci_memoize once for ' '{} to determine speed increase'.format(large_fibonacci)) but in a simple case like yours I'd go with the obvious print( 'Calculating fibonacci and fibonacci_memoize once for', large_fibonacci, 'to determine speed increase')