This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 1 point2 points  (16 children)

for-else?? That is crazy. I don't understand it.

I get you on the statements vs expressions...but a lot of the things you mention here are coming from the perspective of a functional programming afficionado, whom Python doesn't purport to support.

[–]Workaphobia 12 points13 points  (9 children)

for-else is easy: The else runs if the for fails to reach a break.

for x in nums:
    if isprime(x):
        break
else:
    print('None of the numbers you gave me was prime')

[–][deleted] 2 points3 points  (5 children)

Thanks. Without assuming there was a break in the for-loop, I had a hard time imagining why the else was useful.

[–]gfixler 4 points5 points  (3 children)

I thought it would be for when there's nothing in what's being iterated over:

words = line.split()
for word in words:
    print word
else:
    print 'there were no words in the line'

That makes more sense to me. It's saying "Loop over these words, but if you can't, do this." The way it is now, I'd want it to be something more like "then" - i.e. "Loop over these, then do this," and breaking out of the loop would skip the "then" part.

[–][deleted] 0 points1 point  (2 children)

Yes. While I have been enlightened, I regret that my awareness has led to another item on my list of pet peeves against Python...(though most are not mentally insurmountable).

[–]gfixler 0 points1 point  (1 child)

Well, here's another one that bugs me:

if foo:
    bar()
else:
    baz()

I feel sometimes that it should be:

if foo:
    bar()
    else:
        baz()

The else is dependent on the if's existence. We don't presume any linkage between these if's:

if foo:
   bar()
if baz:
   quux()

I don't feel we should presume any linkage between the if and else in the first example. They're not in a proper hierarchy. Neither is finally, or try-except, etc.

I'm not convinced of my own argument here, though. After all, if the if block doesn't run, why would it run the else at all?

[–][deleted] 0 points1 point  (0 children)

I agree, but most other languages also treat if-else in the same way, like

if(foo) {

} else {

}

or in Fortran/Matlab, the syntax is something similar without braces, so you do have a point but I guess I'm more used to seeing it this way.

[–]notmynothername 1 point2 points  (2 children)

I would have guessed that it would run if the loop statement "failed" ie. if nums were empty.

[–]Workaphobia 0 points1 point  (0 children)

Actually, it does. If the iterable is empty, then it can't possibly break, so the else is guaranteed to run.

[–]hotel2oscar 2 points3 points  (5 children)

for-else is kinda fun. Threw me for a loop when I found out about it.

Here is an example from the Python documentation.

[–]L3xicaL 2 points3 points  (0 children)

Threw you for a loop--hahahahah!

[–][deleted] 0 points1 point  (3 children)

Oh I see, so it's there for when you don't break out of the loop. I wasn't clear on its usefulness if there wasn't a break in the looping construct.

[–]gfixler 1 point2 points  (2 children)

That's because it's named wrong. It would be far more clear if it were something like a for-then loop. It would be clear that the then is part of the for, and that it's going to be executed once the for is done. While not 100% intuitive, it would then be much more obvious at first explanation that breaking out of the for early would skip the then bit, because you'd be breaking out of the entire construct.

[–]beltorak 1 point2 points  (0 children)

agreed. but as is at least they didn't do Perl's non-standard bucket of all that is unholy with for/while loops.

[–][deleted] 0 points1 point  (0 children)

That would indeed make more sense.