[Python-Dev] iter alternate form and *args and **kwargs

Steven Bethard steven.bethard at gmail.com
Thu Jun 16 21:28:46 CEST 2005


Steven Bethard wrote:
> I would prefer that the alternate iter() form was broken off into
> another separate function, say, iterfunc(), that would let me write
> Jp's solution something like:
>
> for chunk in iterfunc('', f1.read, CHUNK_SIZE):
>     f2.write(chunk)

Benji York wrote:
> for chunk in iter(partial(f1.read, CHUNK_SIZE), ''):
>     f2.write(chunk)

Steven Bethard wrote:
> Yeah, there are a number of workarounds.  Using partial, def-ing a
> function, or using a lambda will all work.  My point was that, with
> the right API, these workarounds wouldn't be necessary.

Michael Hudson wrote: 
> Well, I dunno.  I can see where you're coming from, but I think you
> could make the argument that the form using partial is clearer to read
> -- it's not absolutely clear that the CHUNK_SIZE argument is intended
> to be passed to f1.read.

True, but the same argument could be made for
unittest.TestCase.assertRaises, and I don't find that confusing at
all.  YMMV, of course.

> Also, the partial approach works better when there is more than one
> callable.

Yeah, I thought of that.  But how often are there multiple callables? 
Just scanning through the builtin functions I see that filter, iter,
map, and reduce all take functions to be called, and none of them take
multiple functions.  (Note that classmethod, property and staticmethod
are not good examples because they don't take callables to be
*called*, they take callables to be *wrapped*.)  OTOH, filter, map and
reduce are all basically deprecated at this point thanks to list
comprehensions and generator expressions, so I guess YMMV.

Steve
-- 
You can wordify anything if you just verb it.
        --- Bucky Katt, Get Fuzzy


More information about the Python-Dev mailing list