On 22:09 Mon 01 Jul , Steven D'Aprano wrote: > On Mon, 01 Jul 2013 20:36:29 +0100, Marcin Szamotulski wrote: > > > Here is another example which I came across when playing with > > generators, the first function is actually quite useful, the second > > generator is the whole fun: > > > > from functools import wraps > > def init(func): > > """decorator which initialises the generator """ > > @wraps(func) > > def inner(*args, **kwargs): > > g = func(*args, **kwargs) > > g.send(None) > > return g > > return inner > > > > @init > > def gen(func): > > x = (yield) > > while True: > > x = (yield func(x)) > > > > > > now if you have function f > > def f(arg): > > return arg**2 > > > > then calling f(5) is the same as > > > > g = gen(f) > > g.send(5) > > > > I think you must be missing an important part of the trick, because > calling f(5) returns 25. It's not: > > @gen > def f(arg): > return arg**2 > > > because that raises TypeError. > -- > Steven > -- > http://mail.python.org/mailman/listinfo/python-list
Sure it does, you're now supposed to use .send method instead of calling it but this is just different syntax. If you want to call it use this : def identity(func): @init def gen(func): x = (yield) while True: x = (yield func(x)) return gen(func).send Now you will get: >>> @identity >>> def f(a): a+1 ... >>> f(0) 1 Best regards, Marcin -- http://mail.python.org/mailman/listinfo/python-list