On this page
  1. contextlib.suppress
  2. Import
  3. Signature & Overview
  4. Usage
  5. Gotchas
  6. REPL Inspect

contextlib.suppress

Prev: contextlib.redirect_stdout | Up: contextlib
Rel: contextlib.AbstractAsyncContextManager, contextlib.AbstractContextManager, contextlib.aclosing, contextlib.asynccontextmanager
Ref: docs.python.org — contextlib.suppress
Tags: #public #python3 #stdlib #contextlib


Import

import contextlib
from contextlib import suppress

Signature & Overview

contextlib.suppress(*exceptions)

Context manager to suppress specified exceptions After the exception is suppressed, execution proceeds with the next statement following the with statement.

Usage

from contextlib import suppress
from pathlib import Path

with suppress(FileNotFoundError):
    Path("definitely_nonexistent_temp_file_xyz").unlink()

Gotchas

REPL Inspect

from contextlib import suppress
help(suppress)

Linked from

Related notes