RE: If/then style question

Discussion in 'Python' started by Rob Richardson, Dec 17, 2010.

  1. -----Original Message-----
    What about,


    def myMethod():
    for condition, exitCode in [
    (cond1, 'error1'),
    (cond2, 'very bad error'),
    ]:
    if not condition:
    break
    else:
    do_some_usefull_stuff() # executed only if the we never hit the
    break statement.
    exitCode = good1

    return exitCode

    ---- I reply -----

    This is interesting, but I don't understand it (which speaks volumes
    about the level of my understanding of Python).

    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?

    Now, the for loop will set condition to cond1 and exitCode to 'error1'.
    Then it checks the contents of the condition variable. But what does
    "not <variable_name>" by itself mean? I'm guessing that it checks that
    the variable refers to an object. So, the first time through, condition
    refers to cond1, the if condition is false, and the else block gets
    executed, and exitCode is changed to refer to good1. The next time
    through the loop, condition is set to refer to cond2 and exitCode is set
    to refer to 'very bad error'. Again, condition is refering to
    something, so the else block is executed and we do useful stuff again,
    which is probably not helpful and could well be harmful. exitCode is
    set to good1, we're finished with the loop, and we return exitCode.

    What happens if we try to do useful stuff, and we can't? Where does the
    error indication get set? And once it does get set, the only way we can
    exit the for loop is for condition to not refer to anything. How can
    that happen?

    Thank you very much for your explanation and your patience with one who
    only uses Python in very simplistic ways.

    RobR
     
    Rob Richardson, Dec 17, 2010
    #1
    1. Advertising

  2. 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?


    --
    Steven
     
    Steven D'Aprano, Dec 17, 2010
    #2
    1. Advertising

  3. Rob Richardson

    Steve Holden Guest

    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/
     
    Steve Holden, Dec 17, 2010
    #3
  4. Rob Richardson

    Tim Golden Guest

    On 17/12/2010 15:53, Steve Holden wrote:

    [... snip example of for-else ...]

    > 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 use this not infrequently, and I like it when it seems to be an
    elegant way to express the code path. But I still misremember from
    time to time and assume that the "else" clause fires when the for
    loop is empty.

    TJG
     
    Tim Golden, Dec 17, 2010
    #4
  5. Rob Richardson

    Mark Wooding Guest

    Steve Holden <> writes:

    > I think the choice of keyword is probably not Guido's crowning
    > language achievement,


    I remember the behaviour by considering a typical application:

    for thing in things:
    if shinyp(thing):
    break
    else:
    raise DullError, 'nothing shiny found'

    In this kind of search loop, `break' signifies a kind of successful
    completion: the `for' loop can be considered to be a test acting over an
    iterable, and `else' therefore denotes the action if the test fails.

    I don't know whether that's the official intuition, or even if there is
    an official intuition, but it works well enough for me. I'm quite fond
    of Python's extra `else' clauses in `for' and (particularly) `try'.

    -- [mdw]
     
    Mark Wooding, Dec 17, 2010
    #5
  6. Rob Richardson

    Steve Holden Guest

    On 12/17/2010 11:13 AM, Tim Golden wrote:
    > On 17/12/2010 15:53, Steve Holden wrote:
    >
    > [... snip example of for-else ...]
    >
    >> 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 use this not infrequently, and I like it when it seems to be an
    > elegant way to express the code path. But I still misremember from
    > time to time and assume that the "else" clause fires when the for
    > loop is empty.
    >

    Yes, that's a common misconception. The classical use is something like

    for item in possibilities:
    if item == target:
    break
    else:
    raise NotFound("Didn't find it")

    Though of course arguably that logic might be expressed in other ways,
    such as

    if target not in possibilities:
    raise NotFound("Didn't find it")

    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/
     
    Steve Holden, Dec 17, 2010
    #6
  7. Tim Golden <> writes:

    > On 17/12/2010 15:53, Steve Holden wrote:
    >
    > [... snip example of for-else ...]
    >
    >> 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 use this not infrequently, and I like it when it seems to be an
    > elegant way to express the code path. But I still misremember from
    > time to time and assume that the "else" clause fires when the for
    > loop is empty.


    I use it from time to time, even though, like you, I used to always be
    unsure when the "else:" suite would be executed. I now remember this
    idiom as the "break else" construct: either the loop breaks, or the
    "else:" suite is executed.

    --
    Arnaud
     
    Arnaud Delobelle, Dec 17, 2010
    #7
  8. Rob Richardson

    Guest

    > I now remember this idiom as the "break else" construct: either the loop breaks, or the "else:" suite is executed.

    A perfect description.

    Malcolm
     
    , Dec 17, 2010
    #8
  9. On Fri, 17 Dec 2010 10:53:45 -0500, Steve Holden wrote about for...else:

    > 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.


    Yes, I find I don't need it often, but it is useful from time to time.

    I wonder whether it would have been more useful to reverse the sense of
    the else, and have it run only if the for loop *didn't* run to
    completion. That seemed more intuitive to me, and I've wanted to do this
    more than once. Here's a toy example:

    for x in sequence:
    if x == "spam":
    print("exiting early")
    break
    elif x == "ham":
    print("exiting early")
    break
    do_something(x)


    would become:

    for x in sequence:
    if x == "spam":
    break
    elif x == "ham":
    break
    do_something(x)
    else:
    print("exiting early")



    > 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.


    Indeed :)




    --
    Steven
     
    Steven D'Aprano, Dec 18, 2010
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?TWlrZQ==?=

    greater then / less then

    =?Utf-8?B?TWlrZQ==?=, Nov 4, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,592
    Kevin Spencer
    Nov 4, 2004
  2. JosephByrns

    Slow, then quick then slow

    JosephByrns, Jul 10, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    2,496
    codezilla94
    Nov 13, 2007
  3. Mufasa
    Replies:
    7
    Views:
    420
    Alexey Smirnov
    Sep 4, 2007
  4. Ken Varn
    Replies:
    0
    Views:
    474
    Ken Varn
    Apr 26, 2004
  5. Replies:
    0
    Views:
    298
Loading...

Share This Page