Hello,

I try to experiment with coroutines and I don't understand why this
snippet doesn't work as expected... In python 2.5 and python 2.6 I get
the following output:

0
Exception exceptions.TypeError: "'NoneType' object is not callable" in
<generator object at 0x7e43f8> ignored

The TypeError exception comes from the pprint instruction...

If i replace the main part with:

==
p1 = dump()
p2 = sort(p1)
for item in my_list: p2.send(item)
==

it works as expected.

I don't understand what is goind wrong. Has someone an explanation for
this issue?

Thanks,

Jérôme


===
from functools import wraps
from pprint import pprint
import random

def coroutine(f):
    @wraps(f)
    def start(*args, **kwargs):
        res = f(*args, **kwargs)
        res.next()
        return res
    return start

@coroutine
def sort(target):
    l = []

    try:
        while True:
            l.append((yield))
    except GeneratorExit:
        l.sort()
        for item in l:
            target.send(item)

@coroutine
def dump():
    while True:
        pprint((yield))

if __name__ == "__main__":
    my_list = range(100)
    random.shuffle(my_list)

    p = sort(dump())

    for item in my_list:
        p.send(item)
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to