using generators with format strings

marduk marduk at python.net
Wed Jul 21 22:21:50 CEST 2004


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