Making string-formatting smarter by handling generators?

Tim Chase python.list at
Wed Feb 27 17:23:49 CET 2008

Is there an easy way to make string-formatting smart enough to 
gracefully handle iterators/generators?  E.g.

   transform = lambda s: s.upper()
   pair = ('hello', 'world')
   print "%s, %s" % pair # works
   print "%s, %s" % map(transform, pair) # fails

with a """
TypeError:  not enough arguments for format string

I can force it by wrapping the results of my generator in a call 
to tuple() or list()

   print "%s, %s" % tuple(map(transform, pair))

but it feels a bit hackish to me.

I find I hit it mostly with calls to map() where I want to apply 
some transform (as above) to all the items in a list of 
parameters such as

   "%s=%s&%s=%s" % map(urllib.quote, params)

Any suggestions?  (even if it's just "get over your hangup with 
wrapping the results in list()/tuple()" :)



More information about the Python-list mailing list