string formatting

Terry Reedy tjreedy at udel.edu
Fri May 6 16:54:17 EDT 2011


On 5/6/2011 3:22 PM, harrismh777 wrote:

> I don't really like the old style, not because there is anything wrong
> with it,

There is in that it special cases tuples. For instance, a message 
function like

def emsg(x):
   print("The following object caused a proplem: %s" % x)

raises "TypeError: not all arguments converted during string formatting"
if x is a tuple with other than 1 member and extracts x[0] if there is 
just one. Neither is the desired behavior. That has been a problem (and 
sometimes a debugging puzzle) in real code One has to remember to write 
something like

def emsg(x):
   if isinstance(x,tuple):
     x = (x,)
   print(The following object caused a proplem: %s" % x)

Guido sees that his original design is a bit of a bug trap, which is one 
reason he wanted to replace it.

-- 
Terry Jan Reedy




More information about the Python-list mailing list