In some algorithms, an else
clause may be more important than an if
clause. This happens when an algorithm is designed to handle a certain set of conditions—the happy path—by default. All of the other non-happy-path conditions require some exceptional processing.
When the default condition is relatively clear and easy to write, but there's no processing required for the condition, we have a syntax issue in Python. The interesting processing belongs to an else
clause, but we have no real code for the initial if
clause. Here's a typical pattern shown with invalid syntax:
if happy_path(x): # nothing special required else: some_special_processing(x) # Processing Continues
The happy_path()
condition confirms that the default processing will work. There's no actual processing do be done when this is true. Since we don't want to do anything, what do we write in the if
clause?
The preceding code is invalid Python. We can't have an empty suite in the if
clause...