How to do this in Python?

Tim Chase python.list at
Wed Mar 18 11:23:16 CET 2009

>>>   def chunk_file(fp, chunksize=10000):
>>>     s =
>>>     while s:
>>>       yield s
>>>       s =
>> Ah.  That's the Pythonesque way I was looking for.
> That's not pythonic unless you really do need to use
> chumk_file() in a lot of places (IMO, more than 3 or 4).  If it
> only going to be used once, then just do the usual thing:

Different strokes for different folks -- my reuse threshold tends 
towards "more than once".  So even a mere 2 copies of the same 
pattern would warrant refactoring out this idiom.

Thanks also to those in the thread that have modeled the new 
next() sentinel syntax -- nifty.


More information about the Python-list mailing list