Python/Python 자료실

Python 3.10 릴리즈 노트 #1. Parenthesized Context Manager

Chipmunks 2022. 2. 6.
728x90

안녕하세요, 다람쥐입니다.

Python 3.10.2 를 릴리즈한 지 하루가 지났습니다.

Python 3.9 버전과 비교해서 Python 3.10 에 어떤 점이 달라졌는지 알아보고자 합니다.

Parenthesized Context Manager

with문을 괄호화 함께 with (...) 처럼 사용할 때 컨텍스트 매니저를 여러 줄에 걸쳐 사용할 수 있도록 합니다.
이전 import 구문에서 여러 줄에 걸쳐 선언했던 것처럼 컨텍스트 매니저를 여러 줄에 선언하고 포맷팅도 할 수 있습니다.

with (CtxManager() as example):
    ...


with (
    CtxManager1(),
    CtxManager2()
):
    ...


with (CtxManager1() as example,
      CtxManager2()):
    ...


with (CtxManager1(),
      CtxManager2() as example):
    ...


with (
    CtxManager1() as example1,
    CtxManager2() as example2
):
    ...


with (
    CtxManager1() as example1,
    CtxManager2() as example2,
    CtxManager3() as example3,
):
    ...

as 를 혼용해서 사용해도 무방합니다.

PEP 617 - New PEG parser for CPython 에서 새로운 파이썬 문법 파서를 도입하여 위 같은 멀티라인 컨텍스트 매니저가 가능해졌습니다.

 

참고 문서

https://docs.python.org/ko/3/whatsnew/3.10.html#summary-release-highlights

 

What’s New In Python 3.10 — Python 3.10.2 문서

Parenthesized context managers Using enclosing parentheses for continuation across multiple lines in context managers is now supported. This allows formatting a long collection of context managers in multiple lines in a similar way as it was previously pos

docs.python.org

https://www.python.org/dev/peps/pep-0617/#background-on-peg-parsers

 

PEP 617 -- New PEG parser for CPython

The official home of the Python Programming Language

www.python.org

 

댓글