pre-PEP: Standard Microthreading Pattern

Michele Simionato michele.simionato at gmail.com
Wed May 2 01:28:08 EDT 2007


On May 1, 10:58 pm, dus... at v.igoro.us wrote:

>    def fibonacci(n):
>        latest, i = (1, 1), 2
>        if n < 1:
>            raise ValueError                    # raise exception
>        while i < n:
>            latest = (latest[1], latest[0] + latest[1])
>            yield                               # cooperative yield
>        raise StopIteration(latest[1])          # return value

There is an infinite loop here!

>    def fibsquared(n):
>        try:
>            fibn = (yield fibonacci(n)) ** 2    # function call
>        except ValueError:                      # catch exception
>            print "Sorry, cannot calculate fibsquared of", n
>        else:
>            print "fibsquared of", n, "is", fibn

It is not clear to me if you mean here

F(yield <genobj>)

as a a shortcut for

for el in <genobj>: yield F(el)

In other words, is ``fibsquared(n).next()`` should return a value or a
generator object??

BTW, in spite of having a great tradition, the Fibonacci example sucks
as a motivation
for microthreading programming. You could show a simple state machine
instead.

      Michele Simionato




More information about the Python-list mailing list