% formatting (was Re: Python equivalent of "lynx -dump"?)
aahz at netcom.com
Wed Mar 29 16:58:48 CEST 2000
In article <eb93es8ja9tbrom92qp8psij1r8jmcigot at 4ax.com>,
Tim Roberts <timr at probo.com> wrote:
>lewst <lewst at yahoo.com> wrote:
>> lynxcmd = "lynx -dump %s" %url
>I hope you do not mind if I ask a Python style question unrelated to your
>issue. That source line contains an idiom I see often in the Python code,
>and I do not understand why. To me, it seems wasteful to use the %
>operator where simple string concatenation will do the same job more
>quickly. For example, isn't this just as clear?
> lynxcmd = "lynx -dump " + url
In this particular case, it is as clear (and if I were writing the code,
I might well do it). However, there are two caveats:
* As was already pointed out, a statement like
sqlCmd = "update foo set bar = '%s' where baz = '%'" % (bar,baz)
is much more complicated using "+" concatenation.
* Believe it or not, "+" is actually slower than "%". Because strings
are immutable, the performance of a tight loop with "+" is *horrible*.
(You have to copy the string each time.) This tends to train Python
programmers into avoiding "+" for strings.
--- Aahz (Copyright 2000 by aahz at netcom.com)
Androgynous poly kinky vanilla queer het <*> http://www.rahul.net/aahz/
Hugs and backrubs -- I break Rule 6
Member of the Groucho Marx Fan Club --Aahz
More information about the Python-list