DEV Community

Cover image for The Strange 'else' in Python
Oleksii Lytvynov
Oleksii Lytvynov

Posted on

The Strange 'else' in Python

Else in Conditional Statements

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')
Enter fullscreen mode Exit fullscreen mode

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.

Else with Loops

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')
Enter fullscreen mode Exit fullscreen mode

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')
Enter fullscreen mode Exit fullscreen mode

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')
Enter fullscreen mode Exit fullscreen mode

Else in Exception Handling

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()
Enter fullscreen mode Exit fullscreen mode

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.

Combining Else with Loops and Exception Handling

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.

from random import random


class Element:
    pass


class ElementNotFoundException(Exception):
    pass


class Driver:

    def __init__(self):
        pass

    def find_element(self, locator: str):
        print(f"Finding element: {locator}")
        if random() < 0.5:
            return Element()
        else:
            raise ElementNotFoundException

    def smart_wait(self, locator: str, timeout: float, step: float):
        raise NotImplementedError
Enter fullscreen mode Exit fullscreen mode

Here’s one approach to implement this method:

  • Trying to find the element as long as the timeout hasn't elapsed.
  • If the element is found, exit the loop.
  • If the element isn’t found, wait for the step interval.
  • Raise an ElementNotFoundException if the timeout is exceeded. Here’s a straightforward implementation:
from time import sleep, monotonic
    def smart_wait(self, locator: str, timeout: float, step: float):
        start_time = monotonic()
        current_time = start_time
        while current_time - start_time < timeout:
            try:
                self.find_element(locator)
                break
            except ElementNotFoundException:
                sleep(step)
                current_time = monotonic()
        if current_time - start_time >= timeout:
            raise ElementNotFoundException
Enter fullscreen mode Exit fullscreen mode

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:

POLL_FREQUENCY: float = 0.5  # How long to sleep in between calls to the method
IGNORED_EXCEPTIONS: Tuple[Type[Exception]] = (NoSuchElementException,)  # default to be ignored.
class WebDriverWait(Generic[D]):
    def __init__(
        self,
        driver: D,
        timeout: float,
        poll_frequency: float = POLL_FREQUENCY,
        ignored_exceptions: Optional[WaitExcTypes] = None,
    ):

    # ...

    def until(self, method: Callable[[D], Union[Literal[False], T]], message: str = "") -> T:
        """Calls the method provided with the driver as an argument until the \
        return value does not evaluate to ``False``.

        :param method: callable(WebDriver)
        :param message: optional message for :exc:`TimeoutException`
        :returns: the result of the last call to `method`
        :raises: :exc:`selenium.common.exceptions.TimeoutException` if timeout occurs
        """
        screen = None
        stacktrace = None

        end_time = time.monotonic() + self._timeout
        while True:
            try:
                value = method(self._driver)
                if value:
                    return value
            except self._ignored_exceptions as exc:
                screen = getattr(exc, "screen", None)
                stacktrace = getattr(exc, "stacktrace", None)
            if time.monotonic() > end_time:
                break
            time.sleep(self._poll)
        raise TimeoutException(message, screen, stacktrace)
Enter fullscreen mode Exit fullscreen mode

Now, let’s rewrite this method using else for both exception handling and loops:

  1. Exception could be raised only in line self.find_element(locator). Exit from loop should be performed in case when exception wasn't raised. So we could move break to else block.
  2. Our method should raise exception if loop was exited not because of break. So we could move exception raising to else clause of the loop.
  3. If you perform transformation 1 and 2 consequentially, you see that current time could be taken only in loop condition.

Completing these transformations, we obtain a method that uses the else statement for both exception handling and the loop:

from time import sleep, monotonic
    def smart_wait(self, locator: str, timeout: float, step: float):
        start_time = monotonic()
        while monotonic() - start_time < timeout:
            try:
                self.find_element(locator)
            except ElementNotFoundException:
                sleep(step)
            else:
                break
        else:
            raise ElementNotFoundException
Enter fullscreen mode Exit fullscreen mode

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 👻

Top comments (0)