1. 파이썬 3.10은 파이썬2.7과의 호환성을 이제 완전히 버린다.

3.3부터 deprecated 오류를 띄우던 것들을 싹 다! 전부 버린다.

(예: collections.abc.Mapping 대신 collections.Mapping 으로 접근 가능했으나 3.10부터는 사라짐)



2. with as 컨텍스트문 안에서 여러 줄을 설정할 수 있다.

예시)

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

동시에 여러개의 컨텍스트를 처리해야 할 때 효과적으로 코드를 짤 수 있게 되었다.



3. 여러가지 에러 문구가 개선되었다.


예) 신택스 에러를 표시해주는 위치와 에러문구 변경사항

이전:

my_dict = {1:3, 5:7

some_other_code = foo()

>> invalid syntax


변경됨:

my_dict = {1:3, 5:7

some_other_code = foo()

>> '{' was never closed


예) 이제 Indentati를 통해 들여쓰기와 관련한 오류를 폭넓게 처리해준다

그냥 중괄호 추가하면 안되냐? 시바꺼



예) 프로퍼티 오류가 날 때 유사한 이름을 제안해준다

my_tuple = collections.namedtuuple()

>> AttributeError: module 'collections' has no attribute 'namedtuuple'. Did you mean: namedtuple?



4. 타입 힌팅을 여러 개로 사용할 수 있다.

예)

def square(number: int | float) -> int | float:

return number ** 2


이외에도 타입 힌팅 관련한 내용이 많이 추가되었다.

TypeGuard를 통해서 특정 함수의 type narrowing을 표시할 수 있다던가...

코드가 상당히 드러워 보이지만 걱정 안해도 된다. 정적 분석기 안쓰면 신경 안써도 되는 내용들이다.

애초에 정적 분석할거면 타입스크립트 쓰지.



5. switch case 문이 추가되었다!!!

예) http 오류코드 처리

def http_error(status):

match status:

case 400:

return "Bad request"

case 404:

return "Not found"

case 418:

return "I'm a teapot"

case _:

return "Something's wrong with the internet"


예) 여러 case를 한번에 처리

def barking_dog_dialogue(name_human: str):

match name_human:

case "Tom" | "Henry" | "Paul" | "Helen":

print(f"Hi {name_human}.")

case _:

print("Woof.")


예) 변수로 언패킹해서 사용

def barking_dog_dialogue(name_human: str):

match name_human:

case "Tom" | "Henry" | "Paul" | "Helen":

print(f"Hi {name_human}.")

case (human_A, human_B):

print(f"Hi {human_A} and {human_B}.")

case _:

print("Woof.")


barking_dog_dialogue(("Tom", "Jenny"))

>> Hi Tom and Jenny.


예) 별표를 사용해서 리스트로 묶어서 받는 것도 가능하다.


예) 특정 case에 대해서만 변수로 사용

match point: # point는 (x, y) 형태의 정수 tuple 임

case (0, 0):

print("Origin")

case (0, y):

print(f"Y={y}")

case (x, 0):

print(f"X={x}")

case (x, y):

print(f"X={x}, Y={y}")

case _:

raise ValueError("Not a point")


예) 와일드카드

match test_variable:

case ('warning', code, 40):

print("A warning has been received.")

case ('error', code, _):

print(f"An error {code} occurred.")



6. 이제 None 의 타입은 NoneType 이다.

그전까진 뭐였냐고? 'NoneType' 이라는 이름의 객체였다.

그래서 그전까지는 isNone 으로 체크하거나, 'is None' 구문을 사용해야 했다.



7. str(), bytes(), bytearray() 생성자가 더욱 빨라진다.

작은 문장의 경우 30~40%의 성능 향상폭을 보인다고 한다.



8. """주석""" 형태를 사용할 때 더이상 문자열 객체를 생성하지 않는다.

그냥 처음부터 여러줄 주석 추가하는게 그렇게 어려웠냐 씨빨새끼들아




이외에 몇가지 내장함수의 이름이 바뀌고.. 구현이 바뀌고.. 성능이 소폭 향상되고...

이것으로 파이썬3.10rc2 리뷰를 마친다.