wow! (generators) - suggestion (yield saved)

Terry Reedy tjreedy at home.com
Sun Aug 5 10:30:18 EDT 2001


"Roman Suzi" <rnd at onego.ru> wrote in message
news:mailman.996997688.23101.python-list at python.org...

> from __future__ import generators
> import calendar, string
> def calt(year, month):
>     month_cal = calendar.monthcalendar(year, month)
>     yield """<B>%s.%s</B>""" % (month, year)
>     yield """<TABLE BORDER=1>"""
>     for week in month_cal:
>       yield """\n<TR>"""
>       for day in week:
>         yield """<TD>%s</TD>""" % (day or " ")
>       yield """</TR>"""
>     yield """</TABLE>"""
>
> print string.join(calt(2001, 8), "")
> --------------------------------------------------------
>
> I like generators :-)

I like generators too, but here you could remove the def and print
directly instead of yield.
Or, append and join the list of strings withing the function, since
they are useless as individual items, and have no need to be yielded
as such.  But the above is arguably better than the suggestions to use
string+= instead (about 35 times), which uselessly copies the growing
string with each addition.

Terry J. Reedy






More information about the Python-list mailing list