using generators with format strings

Calvin Spealman calvin at ironfroggy.com
Mon Jul 26 12:54:50 EDT 2004


The generators are not list-type objects, but iterators. Because the %
operator does not operate on iterators directly (because, presumably, you
may be wanting to print the iterator itself, not the items it iterates
over), you must construct a list out of it, which can be done very easily,
as you can see.

x = "Hello, %s, this is a %s with %s and %s on top of %s" % [ i for i in
myvalues()]
y = "Yes it's true that %s has way too many %s's" % [i for i in myvalues()]

marduk wrote:

> I have a weird request.
> 
> I want to be able to say
> 
> def myvalues():
>     while True:
>         # stuff that determines a new somevalue
>         yield somevalue
> 
> x = "Hello, %s, this is a %s with %s and %s on top of %s" % myvalues()
> y = "Yes it's true that %s has way too many %s's" % myvalues()
> 
> I was hoping that myvalues() would be iterated over, but instead the
> interpreter gives me a "TypeError: not enough arguments for format string"
> error.  I tried tuple(myvalues()) and I think that kinda works but of
> course myvalues goes into an infinite loop. myvalues will not know before
> hand how many times it will be called.
> 
> Is there actually a simple way of doing this that I'm overlooking?
> 
> 
> 
> ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet
> News==---- http://www.newsfeed.com The #1 Newsgroup Service in the World!
> >100,000 Newsgroups ---= 19 East/West-Coast Specialized Servers - Total
> Privacy via Encryption =---

-- 
 




More information about the Python-list mailing list