The pass statement as a placeholder
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 ...
Get Python Essentials now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.