티스토리 뷰
❓ with 절 왜필요한가
python을 사용하다보면 with 절 내에서 처리하는 경우가 많다.
특히 file 제어나 db 제어 할 때 많이 쓰이는데, 다음 파일을 열고 읽는 예제를 확인해보자.
with open("log.txt") as f:
print(f.readline)
이 코드는 다음과 같은 기능을 하는 코드이다.
f = None
try:
f = open("log.txt")
print(f.readline)
finally:
if f is Not None:
f.close()
file을 열고 닫지 않은 경우 memory 누수가 발생하고 이러한 경우를 방지하기 위해 편리하게 나온 문법이 with 절이다.
❓ with 절은 아무 객체에나 쓸 수 있나?
with 절에 대해서 잘 모르는 경우 직면하는 최초 궁금증이라고 생각된다.
with 절은 아무 객체에나 쓸 수 있을까?
당연히 아니다. 특정 함수를 구현한 객체에만 사용가능하다.
바로 __**entry**__ __**exit**__ 이다.
class MyContext:
def __enter__(self):
print("enter")
return "value"
# 인자
# # exc_type: 예외타입
# # exc_value: 예외값
# # traceback: 트레이스 정보
def __exit__(self, exc_type, exc_value, traceback):
print("exit")
with MyContext() as ctx:
print(ctx)
# 출력값
enter
value
exit
함수 정의부(시그니처)는 위와 같이 동일하게 작성한다. (duck typing 룰을 따르지만 pylance 에러가난다😢)
이러한 함수를 구현한 객체를 context manager로 칭한다.
🎯 @contextmanager 활용
python에서는 context manager를 쉽게 구현하기위해서 데코레이터를 제공하는데,
바로 @contextmanager 이다.
from contextlib import contextmanager
@contextmanager
def my_context():
print("enter")
yield "value"
print("exit")
yield 구문과 결합해서 사용하면 정확히 같은 동작을 수행한다.
🎯 자주 사용하는 Context Manager
파일 처리
with open("file.txt", "r") as f:
data = f.read()
DB Connection / Transaction 관리
with db.connect() as conn:
with conn.cursor() as cursor:
cursor.execute("SELECT * FROM table")
Lock / Thread 동기화
from threading import Lock
lock = Lock()
with lock:
critical_section()
timer 구현
import time
from contextlib import contextmanager
@contextmanager
def timer():
start = time.perf_counter()
yield
end = time.perf_counter()
print(f"Elapsed: {end - start:.4f}s")
# 사용
with timer():
heavy_function()
이상으로 with 절에 대해서 깊숙히 알아보았다.
앞으로는 with 절을 만나면 반갑게 인사하자 😄
'python' 카테고리의 다른 글
| [python] Pydantic 정리 (0) | 2026.03.03 |
|---|---|
| [FastAPI] 초기 구성 및 패키지 설치 (0) | 2026.02.25 |
| [Python] Jinja, Blueprint를 활용해 웹 페이지 컴포넌트화 하기 (0) | 2022.08.22 |
| [python] python -m pip install vs pip install (0) | 2022.08.18 |
| [Python] VS Code에 python 환경 구성하기 (0) | 2022.08.16 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- python venv 구성
- Composable vs Component
- docker mssql
- python Pydantic
- Composable vs Class
- docker mssql 이미지 생성
- vue watch 위험성
- vue watch 대체
- vue watch 문제점
- oracle 19c 설치
- nuxt3 프로젝트 설정
- Pydantic 기능
- 외래키 삭제
- Oracle Database 19c install
- nuxt3 structure
- docker mssql create database
- 스파르타 코딩클럽
- 스마트피싱보호_캠페인
- 티스토리챌린지
- 의존성 패키지 관리
- nuxt3 eslint prettier 설정
- nuxt3 quasar 설정
- Oracle Database 19C 설치
- 오블완
- Pydantic 기초
- unplugin-auto-import
- unmounted document.addlistener
- Compoent
- vue 리팩토링
- FastAPI 초기 구성
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| 26 | 27 | 28 | 29 | 30 |
글 보관함

