컨텍스트 관리자
Coddy Python 여정의 객체 지향 프로그래밍 섹션에 포함된 레슨. 64개 중 41번째.
Context managers를 사용하면 필요한 시점에 정확하게 resources를 할당하고 해제할 수 있습니다. 오류가 발생하더라도 적절한 정리를 보장합니다.
다음은 with 문을 사용하는 가장 일반적인 예입니다:
with open('example.txt', 'w') as file:
file.write('Hello, world!')
# 파일은 여기서 자동으로 닫힙니다file은 블록이 끝난 후 exception이 발생하더라도 automatically 닫힙니다.
__enter__ 및 __exit__ 메서드를 구현하여 나만의 컨텍스트 관리자를 만드세요:
class MyContext:
def __enter__(self):
print("Entering the context")
return self
def __exit__(self, exc_type, exc_val, exc_tb):
print("Exiting the context")
return False # 예외를 억제하지 마세요사용자 정의 컨텍스트 관리자를 사용하세요:
with MyContext() as ctx:
print("Inside the context")출력:
Entering the context
Inside the context
Exiting the context데이터베이스 연결을 위한 더 실용적인 컨텍스트 매니저를 만들어 보세요:
class DatabaseConnection:
def __init__(self, db_name):
self.db_name = db_name
self.connection = None
def __enter__(self):
print(f"Connecting to {self.db_name}")
self.connection = f"Connection to {self.db_name}"
return self.connection
def __exit__(self, exc_type, exc_val, exc_tb):
print(f"Closing connection to {self.db_name}")
self.connection = None
with DatabaseConnection("users_db") as conn:
print(f"Using {conn}")
print("Performing database operations...")context managers에서 exceptions 처리하기:
class SafeContext:
def __enter__(self):
print("Setting up resources")
return self
def __exit__(self, exc_type, exc_val, exc_tb):
print("Cleaning up resources")
if exc_type:
print(f"An exception occurred: {exc_val}")
return False # 예외를 억제하지 않음
with SafeContext():
print("Working with resources")
# raise ValueError("Something went wrong") # 테스트하려면 주석 해제출력:
Connecting to users_db
Using Connection to users_db
Performing database operations...
Closing connection to users_db
Setting up resources
Working with resources
Cleaning up resources__exit__ 메서드는 세 개의 매개변수를 받습니다:
exc_type: 예외 유형(또는 None)exc_val: 예외 값(또는 None)exc_tb: 예외 트레이스백(또는 None)
핵심 요점: 컨텍스트 관리자는 __enter__ 및 __exit__ 메서드를 사용하여 resources를 관리합니다. with 문은 이러한 메서드를 automatically 호출하여 올바른 설정과 정리를 보장합니다. 이는 특히 파일, database 연결 및 반드시 정리되어야 하는 기타 resources에 유용합니다.
챌린지
쉬움이 챌린지에서는 컨텍스트 블록에 들어갈 때와 나갈 때 사이의 경과 시간을 측정하는 컨텍스트 관리자 클래스를 구현합니다.
timer.py-Timer클래스 구현을 포함합니다(편집할 파일입니다)driver.py- 포괄적인 테스트 사례를 포함합니다(수정하지 마세요)
timer.py에서 TODO 주석을 따라 Timer 컨텍스트 관리자를 구현하세요. 클래스는 다음을 수행해야 합니다:
- 컨텍스트에 들어갈 때 시작 시간을 기록
- 컨텍스트에서 나갈 때 종료 시간을 기록
- 경과 시간을 계산하고 표시
직접 해보기
from timer import Timer
import time
# 테스트 케이스 핸들러
test_case = input()
if test_case == "basic_test":
# 기본 기능 테스트
with Timer():
# 일부 작업 시뮬레이션
time.sleep(2) # 이렇게 하면 프로그램이 2초 동안 대기합니다
elif test_case == "nested_contexts":
# 중첩된 컨텍스트 매니저 테스트
with Timer():
time.sleep(1)
with Timer():
time.sleep(1)
elif test_case == "exception_handling":
# 컨텍스트 내 예외 처리 테스트
try:
with Timer():
time.sleep(1)
raise ValueError("Test exception")
except ValueError as e:
print(f"Caught exception: {e}")
elif test_case == "zero_sleep":
# 최소 sleep으로 테스트
with Timer():
time.sleep(0.001)
elif test_case == "multiple_timers":
# 순차적으로 여러 타이머 테스트
with Timer():
time.sleep(1)
with Timer():
time.sleep(0.5)
with Timer():
time.sleep(2)
elif test_case == "as_decorator":
# 함수 컨텍스트에서 타이머 테스트
def timed_function():
with Timer():
time.sleep(2)
timed_function()
else:
# 기본 테스트 - 기본 기능
with Timer():
# 일부 작업 시뮬레이션
time.sleep(2) # 이렇게 하면 프로그램이 2초 동안 대기합니다
이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.
객체 지향 프로그래밍의 모든 레슨
직접 연습해 보세요: 온라인 Python 컴파일러