"Weird" Indentation? (Or: is there a for...else construct?)
MRAB
google at mrabarnett.plus.com
Sat Feb 7 09:44:25 EST 2009
Andreas Waldenburger wrote:
> On Sun, 08 Feb 2009 01:28:00 +1100 Steven D'Aprano
> <steve at pearwood.info> wrote:
>
>> Andreas Waldenburger wrote:
>>
>>> It seems that there is a for...else construct. Replacing the inner
>>> if with pass seems to confirm this. The else clause is still
>>> executed.
>> Yes, there is a for...else construct.
>>
> That's something. In 6+ years of Python programming I've never seen or
> heard of this thing. This might be useful, apparently.
>
One use case is:
for x in list_of_items:
if x.value == desired_value:
desired_name = x.name
break
else:
print "Couldn't find %s" % x.value
>
>> [snip]
>>
>>> What's broken here: Python or my brain?
>> Perhaps we should not answer that question.
>>
> I did phrase that rather provocatively, didn't I?
>
> Well thanks. I'll try to learn less noisily in the future. :)
>
More information about the Python-list
mailing list