Help with coroutine-based state machines?

Lulu of the Lotus-Eaters mertz at gnosis.cx
Tue Jun 3 18:32:10 EDT 2003


Alan Kennedy <alanmk at hotmail.com> wrote previously:
|def squares(n):
|    for i in xrange(n):
|        yield n, n*n
|sqgen = squares(100)
|for i, sq in sqgen():
|    print "The square of %d is %d" % (i, sq)

Should be:

    def squares(n):
        for i in xrange(n):
            yield i, i*i    # <-- i
    sqgen = squares(100)
    for i, sq in sqgen:     # <-- no parens
        print "The square of %d is %d" % (i, sq)

--
Keeping medicines from the bloodstreams of the sick; food from the bellies
of the hungry; books from the hands of the uneducated; technology from the
underdeveloped; and putting advocates of freedom in prisons.  Intellectual
property is to the 21st century what the slave trade was to the 16th.





More information about the Python-list mailing list