using generators with format strings

brianc at temple.edu brianc at temple.edu
Wed Jul 21 16:38:55 EDT 2004


List comprehension is a beautiful thing.

x = "Hello, %s, this is a %s with %s and %s on top of %s" %
tuple([myvalues() for i in xrange(5)])

You could even make the xrange argument dependent upon the
number of "%s" in the string. 

---- Original message ----
>Date: Wed, 21 Jul 2004 15:21:50 -0500
>From: marduk <marduk at python.net>  
>Subject: using generators with format strings  
>To: python-list at python.org
>
>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 =---
>-- 
>http://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list