for what are for/while else clauses
JCM
joshway_without_spam at myway.com
Sun Nov 16 20:34:14 EST 2003
John Roth <newsgroups at jhrothjr.com> wrote:
...
>> >> What are the three conditions? I know of two:
>> >>
>> >> 1 Reaching the end of the iteration
>> >> 2 Breaking out
>> > 3. Not executing at all.
>>
>> I see that as an example of #1.
> But it isn't. See what your code looks like with an
> empty file, for example. Or even worse, see what it
> would look like if you have to use a generator where
> you can't test for an empty sequence.
Iterating over an empty file behaves as I expect--the loop terminates
after zero iterations, one time for each line. I'm not sure what you
mean about generators... If you have a generator that never yields
anything:
def g():
if 0:
yield 'nothing'
(is there a better way of writing one?)
then the loop
for x in g():
print x
also terminates after zero iterations.
More information about the Python-list
mailing list