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


Groups > comp.lang.python > #9097

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> <iv7p0o$nup$1@reader1.panix.com>
Date 2011-07-09 08:50 +1000
Message-ID <87box4ie04.fsf@benfinney.id.au> (permalink)
Organization Unlimited download news at news.astraweb.com

Show all headers | View raw


John Gordon <gordon@panix.com> writes:

> I prefer this usage:
>
>   logger.error('%s could not be stored - %s' % \
>     (self.preset_file, sys.exc_info()[1]))

That can be improved by learning two things:

* The backslash-linebreak is ugly and fragile, and almost never needed,
  since Python knows to continue a statement if any bracketing syntax
  (parens, brackets, braces, triple quotes, etc.) is still open.

  So you can continue a statement over multiple lines by introducing
  some bracketing syntax at an appropriate place. In this case, you
  don't even need to add any bracketing syntax, since the function
  parens are still open.

* The ‘%’ string formatting operator is superseded in current Python
  versions by the more flexible ‘format’ method of string objects.

So:

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

I usually prefer to use named placeholders instead of positional, but
this duplicates your original.

-- 
 \           “We have clumsy, sputtering, inefficient brains…. It is a |
  `\     *struggle* to be rational and objective, and failures are not |
_o__) evidence for an alternative reality.” —Paul Z. Myers, 2010-10-14 |
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