Tom Plunket wrote: > Simon Forman wrote: > > > strings have a count() method. > > thanks! > > For enrichment purposes, is there a way to do this sort of thing with > a generator? E.g. something like: > > def SentenceGenerator(): > words = ['I', 'have', 'been', 'to', 'the', 'fair'] > for w in words: > yield w > > message = "%s %s %s %s" > > print message % SentenceGenerator() > > (I ask because the above doesn't work)? > -tom!
If you're asking if there's some way that the generator can know how many formatting fields are in message then the straightforward answer is no. At least not without passing the message to the generator for it to call count() itself, and then you're better off just doing it without the generator. (Actually, IIRC, this was discussed quite recently on this list, and (again IIRC) I think there is some deep voodoo that can do this, but you're much better off without it.) FWIW, in your shoes I would use the trick Justin Azoff posted, i.e.: boiler = 'foo %(modname)s bar %(modname)s baz' message = boiler % {'modname': modname} Peace, ~Simon -- http://mail.python.org/mailman/listinfo/python-list