Live data from Hacker News

The optional “else” in Python loops

shahriar.svbtle.com

11–20 of 50 posts

Re: The optional “else” in Python loops

#11
post #5

Earlier quoted context omitted.

I agree. I use it, but intuitively, I think of else acting the opposite way - if the loop finishes successfully, great! else, do something.

If you're using break to signal success, that is how else acts.

Just to illustrate:

    >>> for item in (1,2):
    	print(item)
    else:
    	print('Done!')

    1
    2
    Done!
    >>> for item in (1,2):
    	print(item)
    	break
    else:
    	print('Done!')
    
    1
    >>>
edit: also:

    >>> for item in ():
    	print(item)
    else:
    	print('Done')
    
    Done

Re: The optional “else” in Python loops

#13
This badly named else is reminiscent of the "result forms" in Lisp loops like `dolist`, `do` or `loop`.

    (loop for x below 10
                finally (princ "iterated over everything, ") (princ "indeed"))
But, of course, these Lisp loops have a result value, and the result forms are an "implicit progn" which produces that value.

Re: The optional “else” in Python loops

#15

I always strongly discourage the use of things like this construct in any language. A language is meant to be read not just written. A non-expert python programmer who encounters this will be confused.

There will always be non-expert programmers of many languages. Avoiding language-specific constructs will not solve the issue.

As far as portability is concerned (if you port software by hand a lot, for example), this is indeed an issue; but if you work with people who write python code, you might as well use everything in your toolbox.

Re: The optional “else” in Python loops

#16

I always strongly discourage the use of things like this construct in any language. A language is meant to be read not just written. A non-expert python programmer who encounters this will be confused.

> who encounters this will be confused

And get it after 30 seconds doc reading?

Re: The optional “else” in Python loops

#17
post #16

I always strongly discourage the use of things like this construct in any language. A language is meant to be read not just written. A non-expert python programmer who encounters this will be confused.

> who encounters this will be confused And get it after 30 seconds doc reading?

In my opinion it's just poorly named.

Re: The optional “else” in Python loops

#19
post #7

Earlier quoted context omitted.

What finally made it intuitive for me: assuming the loop contains an if condition: break, you can consider the else-clause to be the else of the if statement within the loop.

That logic definitely helps me think about it. But it does seem like a switch from the other wording that python uses. Feels like finally would be more intuitive, whereas without thinking about the search loop case else feels like something that would be triggered by a break or return. I wonder if using the feature would be more common if it was more intuitively worded.

It's analogous to the natural termination of a while-else, where the else block is executed after the condition evaluates to false:

    >>> a=1
    >>> while a>0:
        print(4)
        a=a-1
    else:
        print('done')
        
    4
    done
clarity cribbed from here: https://mail.python.org/pipermail/python-list/1999-July/0044...

Re: The optional “else” in Python loops

#20
post #12

Raymond Hettinger, one of the main Python devs, advises that the "else" in this situation should mentally be thought of as "notfound".

Glad you posted this. I came to the comments to post the same thing[1] because thinking of it as "notfound" makes remembering (and comprehending it) easier. :)

[1] https://www.youtube.com/watch?v=OSGv2VnC0go#t=17m12s

Post reply on HN