python 3.8 리뷰
16 Oct 2019 | python3.8 assignment_epressions positional-only_parameters본 포스팅은 python 3.8 변경사항 중 assignment expressions
와 positional-only parameters
의 내용을 정리 했습니다.
assignment expressions
assigment expressions
을 사용하면 if문, while문의 조건부에서 변수를 직접 할당하여 사용할 수 있다.
assigment expressions
미사용
n = len(a)
if n > 10:
print(f"List is too long ( %d elements, expected <= 10)" % n)
assigment expressions
사용
if (n := len(a)) > 10:
print(f"List is too long ({n} elements, expected <= 10)")
- if문 안에서만 사용될 변수라면 위와 같이
assigment expressions
으로 짧게 코딩 할 수 있다.
positional-only parameters
positional-only parameters
를 사용하면 함수 정의시 파라미터 더욱 명시적으로 작성 할 수 있다.
파라미터 정의 부분에 /
추가하게 되면 /
이전 파라미터는 키워드 명시 없이 순서대로 들어가야 하며, 다음 지정된 파리미터에 대해서는 키워드 명시를 강제한다.
positional-only parameters
예시
def f(a, b, /, c, d, *, e, f):
print(a, b, c, d, e, f)
- 가능한 함수 호출 예시
f(10, 20, 30, d=40, e=50, f=60)
- 불가능한 함수 호출 예시
f(10, b=20, c=30, d=40, e=50, f=60) # b 키워드 할당 하면 안됨
f(10, 20, 30, 40, 50, f=60) # e 키워드에 할당 해야함
positional-only parameters
의 장점- 키워드를 명시해야하는 파라미터와 아닌 파라미터를 구분하여 함수를 정의 할 수 있다.
- readablity를 위해 키워드 명시가 불필요한 파라미터에 대해 명시를 하지 않도록 강제할 수 있다.
- 함수 호출시 파라미터 입력에 대한 validation을 강화 할 수 있다.(변수를 잘못된 순서로 할당 한다든지…)
그 외 변경 사항
- Parallel filesystem cache for compiled bytecode files
- f-strings support = for self-documenting expressions and debugging
- PEP 578: Python Runtime Audit Hooks
- PEP 587: Python Initialization Configuration
- Vectorcall: a fast calling protocol for CPython
- Pickle protocol 5 with out-of-band data buffers¶
- etc..