I'd like you to create a context manager.
Context managers use a
with block to bookend a block of code with automatic setup and tear down steps.
Your context manager,
suppress, should suppress exceptions of a given type:
>>> with suppress(NameError): ... print("Hi!") ... print("It's nice to meet you,", name) ... print("Goodbye!") ... Hi! >>> with suppress(TypeError): ... print("Hi!") ... print("It's nice to meet you,", name) ... print("Goodbye!") ... Hi! Traceback (most recent call last): File "<stdin>", line 3, in <module> NameError: name 'name' is not defined >>> x = 0 >>> with suppress(ValueError): ... x = int('hello') ... >>> x 0
What I mean by "suppress" is that if the given exception type is raised, that exception should be caught and muted in a sense. As is common in other exception-handling in Python, child classes of the given exception type should be caught and muted as well.
To solve this exercise, you'll want to lookup how to create a context manager in Python.
This is just a preview of the problem statement.
This exercise includes 3 bonuses, 10 hint links, and automated tests.
To solve this exercise, sign in to your Python Morsels account.
My name is Trey Hunner and I hold Python trainings for teams. I've spent countless hours refining my most interesting Python exercises into Python Morsels, a Python skill-building platform for folks who already know Python.
Python Morsels is design to help you improve your Python skills every week through 1 hour of deliberate practice.
Let me help you write more beautiful Python code. Sign up for Python Morsels to access this exercise and many more.Join Python Morsels ✨