Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 410591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:54:12+00:00 2026-05-12T17:54:12+00:00

I have recently stumbled over a seeming inconsistency in Python’s way of dealing with

  • 0

I have recently stumbled over a seeming inconsistency in Python’s way of dealing with else clauses in different compound statements. Since Python is so well designed, I’m sure that there is a good explanation, but I can’t think of it.

Consider the following:

if condition:
   do_something()
else:
   do_something_else()

Here, do_something_else() is only executed if condition is false, as expected.

Similarly, in

try:
   do_something()
except someException:
   pass:
else:
   do_something_else()
finally:
   cleanup()

do_something_else() is only executed if no exception occurred.

But in for or while loops, an else clause is always executed, whether the contents of the for/while block have been executed or not.

for i in some_iterator:
   print(i)
else:
   print("Iterator is empty!")

will always print “Iterator is empty!”, whether I say some_iterator = [] or some_iterator = [1,2,3]. Same behavior in while-else clauses. It seems to me that else behaves more like finally in these cases. What am I overlooking?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T17:54:12+00:00Added an answer on May 12, 2026 at 5:54 pm

    Well, it depends how you see it. You can look at the elses like this (excuse the screaming, its the only way to make emphasis in code):

    if condition:
       do_something()
    IF THE PREVIOUS CONDITION WAS FALSE:
       do_something_else()
    

    Now, there is an obvious similarity between if/else and try/except/else, if you see the else statement as an else to the except statement. Like this.

    try:
       do_something()
    IF THERE WAS AN EXCEPTION:
       pass:
    IF THE PREVIOUS CONDITION WAS FALSE:
       do_something_else()
    finally:
       cleanup()
    

    Same goes for the else/for:

    IF some_iterator IS NOT EMPTY:
       i = next(some_iterator)
       print(i)
    IF THE PREVIOUS CONDITION WAS FALSE:
       print("Iterator is empty!")
    

    So here we see that the else in some fundamental way do work exactly the same in all three cases.

    But you can also see the else in this way:

    try:
       do_something()
    except someException:
       pass:
    IF NO EXCEPTION:
       do_something_else()
    finally:
       cleanup()
    

    And then it’s not the same anymore, but the else because a sort of “if nothing else”. You can see for/else in the same way:

    for i in some_iterator:
       print(i)
    IF NO MORE ITERATING:
       print("Iterator is empty!")
    

    But then again, considering the elif, then this way of seeing it works for if/else as well:

    if condition:
       do_something()
    elif otherconditaion:
       do_anotherthing()
    IF NO CONDITION WAS TRUE:
       do_something_else()
    

    Which way you want to look at the else is up to you, but in both ways of viewing, else do have similarities in all three cases.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently stumbled upon a problem with selecting relationship details from a 1
I've recently been developing on the django platform and have stumbled upon Django Forms
I have recently stumbled across an issue where the WPF ListView control seems to
I recently stumbled upon a problem where I have a view that can be
I recently stumbled over this post , which introduces the collect method for Scala
I have recently stumbled into this really neat api for showing callOutbubbles in android
I have recently started using PlayN for a hobby project and has now stumbled
Recently, I have stumbled upon the basic understanding about PKI work-in-action process. I have
Recently I stumbled over code such as this: void foo(const Bar* b) { ...
I have recently stumbled uppon this website GrandPixels and found one interesting effect. In

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.