[Python-ideas] for/else syntax
Gerald Britton
gerald.britton at gmail.com
Fri Oct 2 16:05:18 CEST 2009
Nope:
>>> l = []
>>> for i,j in enumerate(l): pass
...
>>> i
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'i' is not defined
"i" is not initialized if "l" is empty.
On Fri, Oct 2, 2009 at 9:54 AM, Yuvgoog Greenle <ubershmekel at gmail.com> wrote:
>
> On Fri, Oct 2, 2009 at 4:46 PM, Gerald
> Britton <gerald.britton at gmail.com> wrote:
>>
>> I can't imagine why I'm still commenting on this thread, since there
>> is no chance that Python will remove the "else" from for/while or put
>> conditions on it, but here is an example of a use that has no break
>> statement:
>>
>> for i, j in enumerate(something):
>> # suite
>> i += 1
>> else:
>> i = 0
>>
>> # i == number of things processed
>>
>
> Sorry, there must be some typo in your code. The "i" is always 0 after the
> loop unless you break.
>>>> for i, j in enumerate(range(10)):
> ... i +=1
> ... else:
> ... i = 0
> ...
>>>> i
> 0
>>>>
> Also, why would one increment the enumeration index "i" by hand?
>
--
Gerald Britton
More information about the Python-ideas
mailing list