On PEP 312: simple implicit lambda
Manuel M. Garcia
mail at manuelmgarcia.com
Sun Feb 16 12:56:15 EST 2003
On Sun, 16 Feb 2003 16:47:11 +0200, Christos "TZOTZIOY" Georgiou
<DLNXPEGFQVEB at spammotel.com> wrote:
(edit)
>Take for example:
>
>data = file_object.read(4096)
>while data:
> # process here
> data = file_object.read(4096)
(edit)
><read_more_data>:
> data = file_object.read(4096)
>while data:
> # process here
> <read_more_data>
The usual idiom used in this case is:
while 1:
data = file_object.read(4096)
if not data: break
# process here
I agree it is not perfect. I never prefer an infinite loop, and I
always try my best to put the loop termination condition as part of
the 'while' statement.
Here is another Python trick inside of a little test program:
# ###################################### #
# tzotzioy_loop01.py
import sys
def store_data(f):
"""adds a .data attribute to a function to
store the last result"""
class _:
def __call__(self):
self.data = f()
return self.data
return _()
file_object = file('tzotzioy_loop01.py')
_readline = store_data( file_object.readline )
i = 0
while _readline():
i += 1
sys.stdout.write('%03i %s' % (i, _readline.data))
# ###################################### #
Using the trick of a temporary class, you have a good place to store
'.data' for later use if the function call is sucessful.
Manuel
More information about the Python-list
mailing list