On PEP 312: simple implicit lambda
Alex Martelli
aleaxit at yahoo.com
Sun Feb 16 12:23:23 EST 2003
Dan Schmidt wrote:
...
> In Python, to code these 'loop and a half' constructs, you have to
> either duplicate some code, as Christos did, or put a conditional
> break halfway through the loop, as I did.
Or (generally best these days) define and use an iterator, e.g.:
def readblocks(fileobject, blocksize=4096):
def readoneblock():
return fileobject.read(blocksize)
return iter(readoneblock, '')
for data in readblocks(file_object):
process(data)
or, use a lambda as iter's first argument to keep the function
you're introducing anonymous, if you prefer that style:
for data in iter(lambda:file_object.read(4096), ''):
process(data)
Alex
More information about the Python-list
mailing list