hands-on lab

Python: Context Managers

Difficulty: Advanced
Duration: Up to 2 hours
Students: 44
Rating: 5/5
Get guided in a real environmentPractice with a step-by-step scenario in a real, provisioned environment.
Learn and validateUse validations to check your solutions every step of the way.
See resultsTrack your knowledge and monitor your progress.

Description

Learn how to ensure your code performs resource cleanup with context managers.

Context managers are objects that create a runtime context where code is executed at the start and end of the context. Context managers are a Pythonic way to perform opening and closing actions such as closing files, connections, etc. This lab introduces context managers through hands-on examples and ends with the chance to practice what you've learned.

Learning Objectives:

  • The use case for context managers
  • Creating context managers from:
    • Classes
    • Callables

Prerequisites:

  • Proficient with:
    • The Python runtime
    • The Python language syntax

Covered topics

Lab steps

Web IDE Review
Context Managers
Demonstrate Your Knowledge