String concatenation vs. string formatting

John Gordon gordon at panix.com
Fri Jul 8 22:23:52 CEST 2011


In <mailman.785.1310156331.1164.python-list at python.org> Andrew Berg <bahamutzero8825 at gmail.com> writes:

> Is it bad practice to use this
> > logger.error(self.preset_file + ' could not be stored - ' +
> > sys.exc_info()[1])
> Instead of this?
> > 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?) 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.

Concatenation feels ugly/clunky to me.

I prefer this usage:

  logger.error('%s could not be stored - %s' % \
    (self.preset_file, sys.exc_info()[1]))

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon at panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"




More information about the Python-list mailing list