473,721 Members | 2,263 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

iter(lambda:f.r ead(8192),'')

what is the difference between iter(lambda:f.r ead(8192), ') and
iter(f.read(819 2),'') ?
Feb 24 '08 #1
3 2831
On Feb 24, 5:11 am, gert <gert.cuyk...@g mail.comwrote:
what is the difference between iter(lambda:f.r ead(8192), ') and
iter(f.read(819 2),'') ?
One does not work, and one is syntactically incorrect:
>>iter(f.read(8 192),'')
Traceback (most recent call last):
File "<pyshell#0 >", line 1, in <module>
iter(f.read(819 2),'')
TypeError: iter(v, w): v must be callable
>>iter(lambda:f .read(8192), ')
SyntaxError: EOL while scanning single-quoted string

To clarify:

f.read(8192) returns the next 8192 bytes of the file in a string, or
whatever is leftover, or an empty string when the file is exhausted.
lambda: f.read(8192) is a function that will return the next 8192
bytes of the file every time it is called.

So iter(f.read(819 2),'') is evaluated as iter(some_strin g, ''). When
iter receives two arguments, it expects the first to be a function,
not a string.

iter(lambda:f.r ead(8192), '') (what you probably meant) is what it
looks like: iter(some_func, '').
Feb 24 '08 #2
aha ok got it :)

Feb 24 '08 #3
En Sun, 24 Feb 2008 10:18:31 -0200, Dustan <Du**********@g mail.com>
escribió:
On Feb 24, 5:11 am, gert <gert.cuyk...@g mail.comwrote:
>what is the difference between iter(lambda:f.r ead(8192), ') and
iter(f.read(81 92),'') ?

iter(lambda:f.r ead(8192), '') (what you probably meant) is what it
looks like: iter(some_func, '').
Just to make it more clear, and guessing the original context:

f = open(...)
for data in iter(lambda: f.read(8192), ''):
do_something_wi th(data)

is the way to iterate over a file in blocks of 8192 bytes each.

--
Gabriel Genellina

Feb 26 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.