Repeat an exception

MRAB python at mrabarnett.plus.com
Thu Feb 4 17:36:09 CET 2010


Jean-Michel Pichavant wrote:
> MRAB wrote:
>> In other words:
>>
>> for attempt in range(2):
>>     try:
>>         spanish_field = translate(english_field, lang_to='es', 
>> lang_from='en')
>>         break
>>     except TranslationError:
>>         pass
>> else:
>>     # Didn't break out of the loop, therefore not successful.
>>     print "Translation failed"
> 
> What the hell is this 'for else' loop !! :D First time I see this 
> statement for years.
> I'd never thought I'd still learn something that basic.
> 
> My first impression is that the  mechansim is not that obvious. MRAB's 
> need for a comment tends to confirm this.
> I'll try to remember that anyway. Nice addition.
> 
The comment is there for the OP. _I_ don't need it! :-)



More information about the Python-list mailing list