If/then style question

Steve Holden steve at holdenweb.com
Fri Dec 17 10:53:45 EST 2010


On 12/17/2010 9:38 AM, Steven D'Aprano wrote:
> On Fri, 17 Dec 2010 09:09:49 -0500, Rob Richardson wrote:
> 
> 
>> First, just to clarify, I don't think the indentation I saw was what was
>> originally posted.  The "else" must be indented to match the "if", and
>> the two statements under "else" are in the else block.  The return
>> statement is indented at the same level as the for statement, so that it
>> will be executed after the for loop exits.  Correct?
> 
> I think that what you are missing is that for-loops can include an else 
> clause too, like this:
> 
> 
>>>> for x in (1,2,3):
> ...     print(x)
> ... else:
> ...     print("finished")
> ...
> 1
> 2
> 3
> finished
>>>>
> 
> 
> The else block runs after the for block, unless you exit the entire block 
> by returning, raising an exception, or using break:
> 
> 
>>>> for x in (1,2,3):
> ...     print(x)
> ...     if x == 3: break
> ... else:
> ...     print("finished")
> ...
> 1
> 2
> 3
>>>>
> 
> 
> Does that clear up what is going on?
> 
> 
This construct appears to be unpopular in actual use, and when it comes
up in classes and seminars there is always interesting debate as people
discuss potential uses and realise there are useful applications.

I think the choice of keyword is probably not Guido's crowning language
achievement, but then since the English keywords don't make natural
sense to those who speak other languages it's at least fair that there
should be one that isn't totally natural to English speakers. A small
price to pay for all the other keywords not being Dutch.

regards
 Steve
-- 
Steve Holden           +1 571 484 6266   +1 800 494 3119
PyCon 2011 Atlanta March 9-17       http://us.pycon.org/
See Python Video!       http://python.mirocommunity.org/
Holden Web LLC                 http://www.holdenweb.com/




More information about the Python-list mailing list