[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Re: Making string-formatting smarter by handling generators?

Gabriel Genellina

2/27/2008 8:12:00 PM

En Wed, 27 Feb 2008 14:23:49 -0200, Tim Chase
<python.list@tim.thechases.com> escribi�:

> 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
> """

Note that your problem has nothing to do with map itself. String
interpolation using % requires either many individual arguments, or a
single *tuple* argument. A list is printed as itself.

py> "%s, %s" % ['hello', 'world']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: not enough arguments for format string
py> "%s" % ['hello', 'world']
"['hello', 'world']"

So the answer is always use tuple(...) as others pointed.

--
Gabriel Genellina