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


Groups > comp.lang.python > #9112

Re: String concatenation vs. string formatting

From Vinay Sajip <vinay_sajip@yahoo.co.uk>
Subject Re: String concatenation vs. string formatting
Date 2011-07-09 11:06 +0000
References <4E17661D.6020307@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.796.1310209616.1164.python-list@python.org> (permalink)

Show all headers | View raw


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

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


Thread

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

csiph-web