Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #49114

Re: Is this PEP-able? fwhile

References <8D03F2B8CF0E7BE-1864-1796B@webmail-m103.sysops.aol.com> <mailman.3760.1372104769.3114.python-list@python.org> <kqajj8$54h$1@dont-email.me> <mailman.3788.1372116912.3114.python-list@python.org> <kqam8g$f7n$1@dont-email.me>
Date 2013-06-25 01:19 +0100
Subject Re: Is this PEP-able? fwhile
From Fábio Santos <fabiosantosart@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.3790.1372120016.3114.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

On 25 Jun 2013 01:08, "alex23" <wuwei23@gmail.com> wrote:
>
> On 25/06/2013 9:35 AM, Fábio Santos wrote:
>>
>>  > I'd probably just go with a generator expression to feed the for loop:
>>  >
>>  >     for X in (i for i in ListY if conditionZ):
>>  >         ....
>>
>> That is nice but it's not lazy. If the condition or the iterables took
>> too long to compute, it would be troublesome.
>
>
> I'm not sure I follow. It's a generator expression, not a list
comprehension, so the condition will be evaluated per item iterated over in
the generator, not across all valid items in ListY at once.
>

for X in (i for i in open('largefile') if is_part_of_header(i)):

The above code would be wasting time on IO and processing. It would load
another line and calculate the condition for every line of the large file
and I just wanted to loop over the few header lines.

itertools.takewhile and fwhile/for..while actually stops the loop when the
condition is not meant, while your example keeps checking the condition
until the end of file, even though it is a generator expression.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: Is this PEP-able? fwhile Ian Kelly <ian.g.kelly@gmail.com> - 2013-06-24 14:12 -0600
  Re: Is this PEP-able? fwhile alex23 <wuwei23@gmail.com> - 2013-06-25 09:14 +1000
    Re: Is this PEP-able? fwhile Fábio Santos <fabiosantosart@gmail.com> - 2013-06-25 00:35 +0100
      Re: Is this PEP-able? fwhile alex23 <wuwei23@gmail.com> - 2013-06-25 10:00 +1000
        Re: Is this PEP-able? fwhile Fábio Santos <fabiosantosart@gmail.com> - 2013-06-25 01:19 +0100
        Re: Is this PEP-able? fwhile wu wei <wuwei23@gmail.com> - 2013-06-25 10:41 +1000
        Re: Is this PEP-able? fwhile Ian Kelly <ian.g.kelly@gmail.com> - 2013-06-24 22:50 -0600
    Re: Is this PEP-able? fwhile rusi <rustompmody@gmail.com> - 2013-06-24 23:04 -0700

csiph-web