[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

iter(lambda:f.read(8192),''

gert

2/24/2008 11:11:00 AM

what is the difference between iter(lambda:f.read(8192), ') and
iter(f.read(8192),'') ?
4 Answers

Dustan

2/24/2008 12:19:00 PM

0

On Feb 24, 5:11 am, gert <gert.cuyk...@gmail.com> wrote:
> what is the difference between iter(lambda:f.read(8192), ') and
> iter(f.read(8192),'') ?

One does not work, and one is syntactically incorrect:

>>> iter(f.read(8192),'')

Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
iter(f.read(8192),'')
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(8192),'') is evaluated as iter(some_string, ''). When
iter receives two arguments, it expects the first to be a function,
not a string.

iter(lambda:f.read(8192), '') (what you probably meant) is what it
looks like: iter(some_func, '').

gert

2/24/2008 1:48:00 PM

0

aha ok got it :)

Gabriel Genellina

2/26/2008 3:40:00 AM

0

En Sun, 24 Feb 2008 10:18:31 -0200, Dustan <DustanGroups@gmail.com>
escribió:
> On Feb 24, 5:11 am, gert <gert.cuyk...@gmail.com> wrote:

>> what is the difference between iter(lambda:f.read(8192), ') and
>> iter(f.read(8192),'') ?
>
> iter(lambda:f.read(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_with(data)

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

--
Gabriel Genellina

Rex the Reaper

1/31/2012 3:47:00 PM

0

On Jan 31, 6:30 am, Steve <stevencan...@yahooooo.com> wrote:
> <snicker> One of the leftists' main problems is that they believe that
> everyone is as thin skinned and self-doubting as they are...

One of Stevie's main problems is that he thinks everyone else is as
sick in the head as he is.