Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #89536

Re: Using + with strings considered bad

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 <python-python-list@m.gmane.org>
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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.79.1430299516.3680.python-list@python.org> (permalink)
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

Show key headers only | View raw


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')

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Using + with strings considered bad Cecil Westerhof <Cecil@decebal.nl> - 2015-04-29 10:29 +0200
  Re: Using + with strings considered bad Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-04-29 10:08 +0100
  Re: Using + with strings considered bad Peter Otten <__peter__@web.de> - 2015-04-29 11:24 +0200
    Re: Using + with strings considered bad Cecil Westerhof <Cecil@decebal.nl> - 2015-04-29 13:17 +0200
    Re: Using + with strings considered bad Cecil Westerhof <Cecil@decebal.nl> - 2015-04-29 14:23 +0200
      Re: Using + with strings considered bad Chris Angelico <rosuav@gmail.com> - 2015-04-29 22:55 +1000
  Re: Using + with strings considered bad Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-04-29 23:15 +1000
    Re: Using + with strings considered bad wxjmfauth@gmail.com - 2015-04-29 08:16 -0700
  Re: Using + with strings considered bad Andrew Berg <aberg010@my.hennepintech.edu> - 2015-04-29 06:40 -0500

csiph-web