On Fri, Jul 8, 2011 at 1:18 PM, Andrew Berg <bahamutzero8...@gmail.com>wrote:
> 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. > String formatting is the One Right Way here. It's fine to use string concatenation for a few things, but the operation is O(n^2) because each concat occurs one at a time: Python allocates space for a string the size of the first 2 things, copies the contents over. Then allocate a string the size of that string plus the third string and copy the contents over. It can get pretty slow if you're building a really big string With string formatting, Python creates a single string large enough to copy all the formatting arguements in and then copies the contents over once. Also, string formatting (especially using the new syntax like you are) is much clearer because there's less noise (the quotes all over the place and the plusses) and it's better for dealing with internationalization if you need to do that.
-- http://mail.python.org/mailman/listinfo/python-list