Marshall T. Vandegrift wrote: > Without the decorator that becomes: > > gen = nextn(2) > print gen.next() # => [0, 1] > print gen.send(3) # => [2, 3, 4] > print gen.send(1) # => [5] > > The former is just that smidgen nicer, and allows you to continue > to make use of argument defaults and varadic arguments if so > desired.
The solution I'd use is a decorator that calls next automatically one time after instantiation. Then you can use send normally, and don't have to care about any initial parameters, which makes the code clearer (initial parameters should be used for setup purposes, but not for the first iteration, IMHO). It'd look like this (from PEP 342, http://www.python.org/dev/peps/pep-0342/): def consumer(func): def wrapper(*args,**kw): gen = func(*args, **kw) gen.next() return gen wrapper.__name__ = func.__name__ wrapper.__dict__ = func.__dict__ wrapper.__doc__ = func.__doc__ return wrapper @consumer def nextn(): ... gen = nextn() print gen.send(2) # => [0, 1] print gen.send(3) # => [2, 3, 4] print gen.send(1) # => [5] Regards, Björn -- BOFH excuse #60: system has been recalled -- http://mail.python.org/mailman/listinfo/python-list