We’ve all written conditional statements and have probably used the complete if-elif-else structure at least once.
For example, when creating a web driver instance for the required browser:
browser = get_browser() if browser == 'chrome': driver = Chrome() elif browser == 'firefox': driver = Firefox() else: raise ValueError('Browser not supported')
This snippet supports testing with Chrome and Firefox, and raises an exception if an unsupported browser is provided.
A lesser-known fact is that Python supports the use of the else clause with loops and exception handling.
Imagine we have a list of words, and we want to print them as long as they start with an uppercase letter. At the end, we want to check whether all words were processed and, if so, perform specific logic.
We might use a flag variable is_all_words_processed, setting it to False if we encounter an invalid word, then checking it outside the loop to execute the logic.
seasons = ['Winter', 'Spring', 'Summer', 'Autumn'] is_all_words_processed = True for season in seasons: if not season.istitle(): is_all_words_processed = False break print(season) if is_all_words_processed: print('All seasons were processed')
Python allows us to avoid the additional variable by placing the logic when all words are valid into the else clause:
seasons = ['Winter', 'Spring', 'Summer', 'Autumn'] for season in seasons: if not season.istitle(): break print(season) else: print('All seasons were processed')
The else block will execute only if the loop completes naturally, without a break. If the loop is interrupted by break, the else clause will not run.
Here’s the same example rewritten with a while loop. With while, the else clause behaves in the same way:
seasons = ['Winter', 'Spring', 'Summer', 'Autumn'] index = 0 while index < len(seasons): if not seasons[index].istitle(): break print(seasons[index]) index += 1 else: print('All seasons were processed')
The else clause can also be used in exception handling. It must come after all except blocks. The code inside the else block will execute only if no exceptions are raised in the try block.
For example, let’s read a file containing numbers in two columns and print their quotient. We need to handle an invalid file name, while any other errors (e.g., converting a value to a number or division by zero) should cause the program to crash (we will not handle them).
file_name = 'input.dat' try: f = open(file_name, 'r') except FileNotFoundError: print('Incorrect file name') else: for line in f: a, b = map(int, line.split()) print(a / b) f.close()
In this example, the try block contains only the code that might raise the caught exception.
The official documentation suggests using the else block to avoid unintentionally catching exceptions raised by code outside the try block. Still, the use of else in exception handling might not feel intuitive.
Here’s an question I posed at interviews.
Suppose we have a Driver class with a method find_element. The find_element method either returns an element or raises an ElementNotFoundException exception. In this example, it’s implemented to randomly return an element or raise an exception with equal probability.
Using basic Python syntax, implement a method smart_wait(self, locator: str, timeout: float, step: float) that checks for an element with the given locator every step seconds. If the element is found within timeout seconds, return; otherwise, raise an ElementNotFoundException exception.
browser = get_browser() if browser == 'chrome': driver = Chrome() elif browser == 'firefox': driver = Firefox() else: raise ValueError('Browser not supported')
Here’s one approach to implement this method:
seasons = ['Winter', 'Spring', 'Summer', 'Autumn'] is_all_words_processed = True for season in seasons: if not season.istitle(): is_all_words_processed = False break print(season) if is_all_words_processed: print('All seasons were processed')
We could shorten the logic a bit by using return instead of break, but let's leave it as i for now.
In fact, this method is implemented in the WebDriverWait class of Selenium - until method:
seasons = ['Winter', 'Spring', 'Summer', 'Autumn'] for season in seasons: if not season.istitle(): break print(season) else: print('All seasons were processed')
Now, let’s rewrite this method using else for both exception handling and loops:
Completing these transformations, we obtain a method that uses the else statement for both exception handling and the loop:
seasons = ['Winter', 'Spring', 'Summer', 'Autumn'] index = 0 while index < len(seasons): if not seasons[index].istitle(): break print(seasons[index]) index += 1 else: print('All seasons were processed')
What can I say... This is one of Python’s lesser-known features. Infrequent use might make it less intuitive to use in every scenario — it can lead to confusion. However, knowing it and applying it effectively when needed is undoubtedly worthwhile.
Happy New Year! ???
P.S. It was really scary ?:
I write articles on my own but translate them using ChatGPT. For translation I removed all code snippets but ChatGPT restores them all ?
The above is the detailed content of The Strange else in Python. For more information, please follow other related articles on the PHP Chinese website!