Python: Context Managers

Lab Steps

lock
Web IDE Review
lock
Context Managers
lock
Demonstrate Your Knowledge

The hands-on lab is part of this learning path

Ready for the real environment experience?

DifficultyAdvanced
Time Limit2h
Students5

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
About the Author
Students95310
Labs26
Courses44
Learning paths56

Ben Lambert is a software engineer and was previously the lead author for DevOps and Microsoft Azure training content at Cloud Academy. His courses and learning paths covered Cloud Ecosystem technologies such as DC/OS, configuration management tools, and containers. As a software engineer, Ben’s experience includes building highly available web and mobile apps. When he’s not building software, he’s hiking, camping, or creating video games.

Covered Topics