Coroutines and argument tupling
Marshall T. Vandegrift
llasram at gmail.com
Wed Aug 15 18:37:53 EDT 2007
Bjoern Schliessmann <usenet-mail-0306.20.chr0n0ss at spamgourmet.com> writes:
>> I'm trying to write a decorator which allows one to produce simple
>> coroutines by just writing a function as a generator expression
>> which re-receives it's arguments as a tuple from each yield.
>
> May I ask why? Passing it the same arguments over and over is no
> use; and there is the send method.
That's what I meant. The wrapper produced by the decorator passes the
arguments back into the generator as a tuple via the `send' method.
>> The ugliness of the ArgPacker class makes me suspect that I should
>> perhaps just manually create and track a generator when I need a
>> function with generator-like properties.
>
> What do you mean? I don't quite understand why you'd have to "track"
> a generator for getting generator-like properties.
Using the trivial `nextn' example from my original post with my
decorator lets you do just:
print nextn(2) # => [0, 1]
print nextn(3) # => [2, 3, 4]
print nextn() # => [5]
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.
-Marshall
More information about the Python-list
mailing list