building strings with variable input

Paul McGuire ptmcg at users.sourceforge.net
Wed Jan 14 08:39:21 CET 2004


"Tim Roberts" <timr at probo.com> wrote in message
news:3rr900dm9s7th7kgq4muj76d5ocs5uep3b at 4ax.com...
> Olaf Meyer <nomail at nospam.net> wrote:
> >
> >I just found out another way ;-) Using the locals() has the disadvantage
> >that I cannot use more complex variable parameters (e.g. certain values
> >of a dictionary). The following works well:
> >
> >cmd = (executable + " -start " + startTime + " -end " + endTime +
> >        " -dir " + options.dir)
>
> Yes, that works, but you should bear in mind that it is slower than the %s
> option.  The "+" operations are all separate interpreter steps, while the
> "%" operation is done in C.
>

On the relative time scales of concatenating 7 strings compared to forking
off a separate process (which I presume is what is to be done with cmd), I'd
go for the more readable representation, to aid in long term
maintainability.

If I have some string concatenation being done in a highly repetitive part
of code, then by all means, replace it with one of the half dozen documented
optimized alternatives.  But if I build a string in order to create a
sub-process, or invoke a database query, or make a remote CORBA invocation,
etc., then these "optimizations" don't really save much time, and instead
distract me/reviewers/testers/maintainers from the important program logic.

-- Paul





More information about the Python-list mailing list