Coroutines and argument tupling
Marshall T. Vandegrift
llasram at gmail.com
Thu Aug 16 02:30:45 EDT 2007
"attn.steven.kuo at gmail.com" <attn.steven.kuo at gmail.com> writes:
> Do you really need a generator or co-routine to do this? Maybe
> you can just use a closure:
For my trivial example, sure -- there are lots of ways to do it. Here's
a slightly better example: the `read' method of a file-like object which
sequentially joins the contents of a sequence of other file-like
objects:
@coroutine
def read(self, size=-1):
data = ''
for file in self.files:
this = file.read(size - len(data))
data = ''.join([data, this])
while this:
if len(data) == size:
_, size = (yield data)
data = ''
this = file.read(size - len(data))
data = ''.join([data, this])
yield data
while True:
yield ''
-Marshall
More information about the Python-list
mailing list