Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: Timer Context Manager | Practice with Context Managers
Python Context Managers
Section 3. Chapter 1
single

single

bookChallenge: Timer Context Manager

Swipe to show menu

Task

Swipe to start coding

Implement a context manager that measures and prints the elapsed time when exiting a context. The context manager should use the context management protocol. The __enter__ method should record the current time. The __exit__ method should record the end time, calculate the elapsed time, and print the duration in seconds.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 3. Chapter 1
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

some-alt