본문 바로가기
IT/Programming Language

[Python] switch -> match

by FreeYourMind 2022. 3. 22.

python은 switch를 대체할 문법으로 match가 있다.

아래와 같이 사용법이 비슷하다.

def http_status(status):
    match status:
        case 400:
            return "Bad request"
        case 401 | 403:
            return "Authentication error"
        case 404:
            return "Not found"
        case _:
            return "Other error"

 

 

출처

https://towardsdatascience.com/the-match-case-in-python-3-10-is-not-that-simple-f65b350bb025

 

'IT > Programming Language' 카테고리의 다른 글

[Python] 행렬 회전  (0) 2022.03.23
[Python] 까먹을 만한 문법 정리  (0) 2022.03.22
Generic이란  (0) 2022.03.06
[Javascript] 비동기 처리 방식  (0) 2022.02.20
[Javascript] closure  (0) 2022.02.20

댓글