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


Groups > comp.lang.python > #9098

Re: String concatenation vs. string formatting

From Ben Finney <ben+python@benfinney.id.au>
Newsgroups comp.lang.python
Subject Re: String concatenation vs. string formatting
References <mailman.785.1310156331.1164.python-list@python.org>
Date 2011-07-09 08:59 +1000
Message-ID <877h7sidjq.fsf@benfinney.id.au> (permalink)
Organization Unlimited download news at news.astraweb.com

Show all headers | View raw


Andrew Berg <bahamutzero8825@gmail.com> writes:

> Is it bad practice to use this
> > logger.error(self.preset_file + ' could not be stored - ' +
> > sys.exc_info()[1])

This is not necessarily bad practice, but there are not many points in
its favour. It's inflexible and makes the eventual formatting harder to
discern.

> Instead of this?
> > logger.error('{file} could not be stored -
> > {error}'.format(file=self.preset_file, error=sys.exc_info()[1]))

With the caveat that the formatting of that line should be using PEP 8
indentation for clarity:

    logger.error(
        '{file} could not be stored - {error}'.format(
            file=self.preset_file, error=sys.exc_info()[1]))

> Other than the case where a variable isn't a string (format() converts
> variables to strings, automatically, right?)

If you don't specify a conversion in the placeholder, it will default to
‘str’, yes.

> and when a variable is used a bunch of times, concatenation is fine,

I don't see any argument for concatenation there; using a variable a
bunch of times works just fine with the ‘format’ method.

> 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.

There is often more than one way to do it. The Zen of Python is explicit
that there should be one obvious way to do it (and preferably only one).

The “OOW” in “TOOWTDI” is not “only one way”, but “one obvious way”.

The presence of multiple ways to format strings (the ‘%’ operator, the
‘format’ method) is for backward compatibility with code written before
the current recommended ‘format’ method.

Backward compatibility is a common reason for more than one way to do it
in Python. It's just preferable that all but one of them should be
non-obvious :-)

-- 
 \            “The whole area of [treating source code as intellectual |
  `\    property] is almost assuring a customer that you are not going |
_o__)               to do any innovation in the future.” —Gary Barnett |
Ben Finney

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


Thread

String concatenation vs. string formatting Andrew Berg <bahamutzero8825@gmail.com> - 2011-07-08 15:18 -0500
  Re: String concatenation vs. string formatting John Gordon <gordon@panix.com> - 2011-07-08 20:23 +0000
    Re: String concatenation vs. string formatting Ben Finney <ben+python@benfinney.id.au> - 2011-07-09 08:50 +1000
      Re: String concatenation vs. string formatting Ben Finney <ben+python@benfinney.id.au> - 2011-07-09 09:15 +1000
    Re: String concatenation vs. string formatting Thorsten Kampe <thorsten@thorstenkampe.de> - 2011-07-09 06:23 +0200
    Re: String concatenation vs. string formatting Andrew Berg <bahamutzero8825@gmail.com> - 2011-07-10 01:45 -0500
      Re: String concatenation vs. string formatting Ben Finney <ben+python@benfinney.id.au> - 2011-07-10 17:07 +1000
      Re: String concatenation vs. string formatting Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-07-10 23:42 +1000
      Re: String concatenation vs. string formatting Roy Smith <roy@panix.com> - 2011-07-10 10:33 -0400
        Re: String concatenation vs. string formatting Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-07-11 08:48 +1000
        Re: String concatenation vs. string formatting Andrew Berg <bahamutzero8825@gmail.com> - 2011-07-10 18:24 -0500
  Re: String concatenation vs. string formatting Billy Mays <noway@nohow.com> - 2011-07-08 16:57 -0400
    Re: String concatenation vs. string formatting Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-07-09 15:30 +1000
      Re: String concatenation vs. string formatting Ian Kelly <ian.g.kelly@gmail.com> - 2011-07-09 00:04 -0600
      Re: String concatenation vs. string formatting Chris Angelico <rosuav@gmail.com> - 2011-07-09 16:16 +1000
      Re: String concatenation vs. string formatting Ian Kelly <ian.g.kelly@gmail.com> - 2011-07-09 00:29 -0600
  Re: String concatenation vs. string formatting Ben Finney <ben+python@benfinney.id.au> - 2011-07-09 08:59 +1000
    Re: String concatenation vs. string formatting Andrew Berg <bahamutzero8825@gmail.com> - 2011-07-08 18:29 -0500
  Re: String concatenation vs. string formatting Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-07-09 15:12 +1000

csiph-web