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


Groups > comp.lang.python > #9112 > unrolled thread

Re: String concatenation vs. string formatting

Started byVinay Sajip <vinay_sajip@yahoo.co.uk>
First post2011-07-09 11:06 +0000
Last post2011-07-09 11:06 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: String concatenation vs. string formatting Vinay Sajip <vinay_sajip@yahoo.co.uk> - 2011-07-09 11:06 +0000

#9112 — Re: String concatenation vs. string formatting

FromVinay Sajip <vinay_sajip@yahoo.co.uk>
Date2011-07-09 11:06 +0000
SubjectRe: String concatenation vs. string formatting
Message-ID<mailman.796.1310209616.1164.python-list@python.org>
Andrew Berg <bahamutzero8825 <at> gmail.com> writes:

> Other than the case where a variable isn't a string (format() converts
> variables to strings, automatically, right?) and when a variable is used
> a bunch of times, concatenation is fine, but somehow, it seems wrong.
> Sorry if this seems a bit silly, but I'm a novice when it comes to
> design. Plus, there's not really supposed to be "more than one way to do
> it" in Python.

In a logging context at least, using the form like

logger.debug("formatting message with %s", "arguments")

rather than

logger.debug("formatting message with %s" % "arguments")

means that the formatting is deferred by logging until it is actually needed. If
the message never gets output because of the logging configuration in use, then
the formatting is never done. This optimisation won't matter in most cases, but
it will in some scenarios.

By the way, logging primarily uses %-formatting instead of the newer
{}-formatting, because it pre-dates {}-formatting. In more recent versions of
Python, all of Python's three formatting styles are supported - see

http://plumberjack.blogspot.com/2010/10/supporting-alternative-formatting.html

Also by the way - Python doesn't say there shouldn't be more than one way to do
things - just that there should be one *obvious* way (from the Zen of Python).

Regards,

Vinay Sajip

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web