Why do this?

Corrado Gioannini gioco at nekhem.com
Thu Oct 5 18:25:17 CEST 2006


On Thu, Oct 05, 2006 at 10:48:36AM +0000, Duncan Booth wrote:
> The other main reason for preferring format strings is that they make it 
> easier to refactor the code. If you ever want to move the message away from 
> where the formatting is done then it's a lot easier to extract a single 
> string than it is to clean up the concatenation.

This is a good point imho. 
I often do things like this:

  sql = a_complex_select_sql % (id_foo, value_bar, ...)
  cursor.execute(sql)

inside the body of a function (or a class method), where
a_complex_select_sql is a string, containing several %s, %d ecc., 
that is defined globally (or somewhere else in the class).

c.
-- 
"Thought is only a flash between two long nights,
                                         but this flash is everything."
                                                          (H. Poincaré)



More information about the Python-list mailing list