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


Groups > comp.lang.python > #32223

Re: better way for ' '.join(args) + '\n'?

From Hubert Grünheidt <hgruenheidt@t-online.de>
Newsgroups comp.lang.python
Subject Re: better way for ' '.join(args) + '\n'?
Date 2012-10-26 18:21 +0200
Organization albasani.net
Message-ID <k6ed9j$bq8$1@news.albasani.net> (permalink)
References <utrpl9-udk.ln1@satorlaser.homedns.org>

Show all headers | View raw


Hi Ulrich,

is this acceptable?

     args = ['foo', 'bar', 'baz']
     args.append('\n')
     line = ' '.join(args)

Cheers,
       Hubert

On 10/26/2012 09:49 AM, Ulrich Eckhardt wrote:
> Hi!
>
> General advise when assembling strings is to not concatenate them
> repeatedly but instead use string's join() function, because it avoids
> repeated reallocations and is at least as expressive as any alternative.
>
> What I have now is a case where I'm assembling lines of text for driving
> a program with a commandline interface. In this scenario, I'm currently
> doing this:
>
>    args = ['foo', 'bar', 'baz']
>    line = ' '.join(args) + '\n'
>
> So, in other words, I'm avoiding all the unnecessary copying, just to
> make another copy to append the final newline.
>
> The only way around this that I found involves creating an intermediate
> sequence like ['foo', ' ', 'bar', ' ', 'baz', '\n']. This can be done
> rather cleanly with a generator:
>
>    def helper(s):
>        for i in s[:-1]:
>             yield i
>             yield ' '
>        yield s[-1]
>        yield '\n'
>    line = ''.join(tmp(args))
>
> Efficiency-wise, this is satisfactory. However, readability counts and
> that is where this version fails and that is the reason why I'm writing
> this message. So, dear fellow Pythonistas, any ideas to improve the
> original versions efficiency while preserving its expressiveness?
>
> Oh, for all those that are tempted to tell me that this is not my
> bottleneck unless it's called in a very tight loop, you're right.
> Indeed, the overhead of the communication channel TCP between the two
> programs is by far dwarving the few microseconds I could save here. I'm
> still interested in learning new and better solutions though.
>
>
> Cheers!
>
> Uli
>

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


Thread

better way for ' '.join(args) + '\n'? Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-10-26 09:49 +0200
  Re: better way for ' '.join(args) + '\n'? Peter Otten <__peter__@web.de> - 2012-10-26 10:58 +0200
  Re: better way for ' '.join(args) + '\n'? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-26 09:36 +0000
  Re: better way for ' '.join(args) + '\n'? Hubert Grünheidt <hgruenheidt@t-online.de> - 2012-10-26 18:21 +0200
  Re: better way for ' '.join(args) + '\n'? Tycho Andersen <tycho@tycho.ws> - 2012-10-26 16:26 -0500
  Re: better way for ' '.join(args) + '\n'? Dave Angel <d@davea.name> - 2012-10-26 17:36 -0400
  Re: better way for ' '.join(args) + '\n'? Tycho Andersen <tycho@tycho.ws> - 2012-10-26 16:42 -0500
    Re: better way for ' '.join(args) + '\n'? Ramchandra Apte <maniandram01@gmail.com> - 2012-11-03 01:26 -0700
    Re: better way for ' '.join(args) + '\n'? Ramchandra Apte <maniandram01@gmail.com> - 2012-11-03 01:26 -0700
  Re: better way for ' '.join(args) + '\n'? Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2012-10-27 11:26 +0200
    RE: better way for ' '.join(args) + '\n'? "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-10-29 22:28 +0000

csiph-web