Created at : 2024-11-17 15:53
Auther: Soo.Y


πŸ“λ©”λͺ¨

python 3.10

1. ꡬ쑰적 νŒ¨ν„΄ λ§€μΉ­

python 3.10μ—μ„œλŠ” μƒˆλ‘œμš΄ match / case문법을 λ„μž…ν•˜μ—¬ ꡬ쑰적 νŒ¨ν„΄ 맀칭을 μ§€μ›ν•©λ‹ˆλ‹€. μ΄λŠ” κ°’μ˜ ν˜•νƒœμ™€ λ‚΄μš©μ„ 기반으둜 λΆ„κΈ° 처리λ₯Ό ν•  수 있게 ν•΄μ€λ‹ˆλ‹€.

def http_status(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"
 
print(http_status(404))  # 좜λ ₯: Not Found

λ”•μ…”λŸ¬λ‹ˆ ν˜•νƒœ match / case 예제

data = {"action": "move", "direction": "north"}
 
match data:
    case {"action": "move", "direction": direction}:
        print(f"Move towards {direction}")
    case {"action": "stop"}:
        print("Stop moving")
    case _:
        print("Unknown action")

2. νƒ€μž… νžŒνŒ… κ°œμ„ : μœ λ‹ˆμ–Έ νƒ€μž…μ— |μ—°μ‚°μž μ‚¬μš©

νƒ€μž… νžŒνŒ…μ—μ„œ μœ λ‹ˆμ–Έ νƒ€μž…μ„ μ •μ˜ν•  λ•Œ 이제 typing.Union λŒ€μ‹  | μ—°μ‚°μžλ₯Ό μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

  • int | str은 int λ˜λŠ” str νƒ€μž…μ„ μ˜λ―Έν•©λ‹ˆλ‹€.
from typing import Union
 
# κΈ°μ‘΄ 방식
def process(value: Union[int, str]) -> None:
    print(value)
 
# μƒˆλ‘œμš΄ 방식
def process(value: int | str) -> None:
    print(value)

3. μ—λŸ¬ λ©”μ‹œμ§€ κ°œμ„ 

python 3.10μ—μ„œλŠ” ꡬ문 였λ₯˜ 및 μ˜ˆμ™Έ λ°œμƒ μ‹œ 더 λͺ…ν™•ν•˜κ³  μƒμ„Έν•œ μ—λŸ¬ λ©”μ‹œμ§€λ₯Ό μ œκ³΅ν•©λ‹ˆλ‹€.

# 잘λͺ»λœ ꡬ문
def func():
    print("Hello"
 
# 이전 λ²„μ „μ˜ μ—λŸ¬ λ©”μ‹œμ§€:
# SyntaxError: unexpected EOF while parsing
 
# Python 3.10의 μ—λŸ¬ λ©”μ‹œμ§€:
# SyntaxError: '('둜 μ—΄λ Έμ§€λ§Œ μΌμΉ˜ν•˜λŠ” ')'κ°€ μ—†μŠ΅λ‹ˆλ‹€.

4. μ»¨ν…μŠ€νŠΈ κ΄€λ¦¬μžμ—μ„œ κ΄„ν˜Έ μ‚¬μš© κ°€λŠ₯

이제 withλ¬Έμ—μ„œ μ—¬λŸ¬ μ»¨ν…μŠ€νŠΈ κ΄€λ¦¬μžλ₯Ό κ΄„ν˜Έλ‘œ λ¬Άμ–΄ μ—¬λŸ¬ μ€„λ‘œ λ‚˜λˆŒ 수 μžˆμŠ΅λ‹ˆλ‹€.

# κΈ°μ‘΄ 방식 (ν•œ 쀄에 μ—¬λŸ¬ μ»¨ν…μŠ€νŠΈ κ΄€λ¦¬μž)
with open("file1.txt") as f1, open("file2.txt") as f2:
    pass
 
# μƒˆλ‘œμš΄ 방식 (κ΄„ν˜Έλ₯Ό μ‚¬μš©ν•˜μ—¬ μ—¬λŸ¬ μ€„λ‘œ λ‚˜λˆ„κΈ°)
with (
    open("file1.txt") as f1,
    open("file2.txt") as f2,
):
    pass

5. typing λͺ¨λ“ˆμ˜ κ°œμ„ 

5.1 λ§€κ°œλ³€μˆ˜ 사양 λ³€μˆ˜ (ParamSpec)

ParamSpec을 μ‚¬μš©ν•˜λ©΄ κ³ μ°¨ ν•¨μˆ˜μ˜ λ§€κ°œλ³€μˆ˜ νƒ€μž…μ„ ν‘œν˜„ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

from typing import Callable, TypeVar, ParamSpec
 
P = ParamSpec('P')
R = TypeVar('R')
 
def add_logging(f: Callable[P, R]) -> Callable[P, R]:
    def wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
        print(f"Calling {f.__name__}")
        return f(*args, **kwargs)
    return wrapper
 
@add_logging
def add(a: int, b: int) -> int:
    return a + b
 
print(add(2, 3))  # 좜λ ₯: Calling add\n5

5.2 νƒ€μž… 별칭(TypeAlias)

TypeAliasλ₯Ό μ‚¬μš©ν•˜μ—¬ νƒ€μž… 별칭을 λͺ…ν™•ν•˜κ²Œ μ •μ˜ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

from typing import TypeAlias
 
UserId: TypeAlias = int
 
def get_user_name(user_id: UserId) -> str:
    return "User Name"
 
print(get_user_name(123))  # 좜λ ₯: User Name

6. zip() ν•¨μˆ˜μ˜ 엄격 λͺ¨λ“œ 지원

zip() ν•¨μˆ˜μ— μƒˆλ‘œμš΄ strict λ§€κ°œλ³€μˆ˜κ°€ μΆ”κ°€λ˜μ–΄, 길이가 λ‹€λ₯Έ μ΄ν„°λŸ¬λΈ”μ„ 전달할 λ•Œ 였λ₯˜λ₯Ό λ°œμƒμ‹œν‚¬ 수 μžˆμŠ΅λ‹ˆλ‹€.

  • strict=Trueλ₯Ό μ§€μ •ν•˜λ©΄ μ „λ‹¬λœ λͺ¨λ“  μ΄ν„°λŸ¬λΈ”μ˜ 길이가 λ™μΌν•˜μ§€ μ•ŠμœΌλ©΄ ValueErrorκ°€ λ°œμƒν•©λ‹ˆλ‹€.
# κΈ°μ‘΄ 방식
list(zip([1, 2, 3], ['a', 'b']))  # 좜λ ₯: [(1, 'a'), (2, 'b')]
 
# strict=True μ‚¬μš©
list(zip([1, 2, 3], ['a', 'b'], strict=True))  # ValueError λ°œμƒ

7. str νƒ€μž…μ˜ removeprefix() 및 removesuffix() λ©”μ†Œλ“œ

python 3.9μ—μ„œ λ„μž…λœ removeprefix()와 removesuffix() λ©”μ†Œλ“œλ₯Ό μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

text = "unbelievable"
 
# 접두사 제거
print(text.removeprefix("un"))  # 좜λ ₯: believable
 
# 접미사 제거
print(text.removesuffix("able"))  # 좜λ ₯: unbeliev

8. statistics λͺ¨λ“ˆμ˜ μƒˆλ‘œμš΄ ν•¨μˆ˜

statistics.quantiles() 데이터 μ§‘ν•©μ˜ λΆ„μœ„μˆ˜λ₯Ό κ³„μ‚°ν•˜λŠ” ν•¨μˆ˜κ°€ μΆ”κ°€λ˜μ—ˆμŠ΅λ‹ˆλ‹€.

  • quantiles()ν•¨μˆ˜λŠ” λ°μ΄ν„°μ˜ λΆ„μœ„μˆ˜λ₯Ό κ³„μ‚°ν•˜μ—¬ 리슀트둜 λ°˜ν™˜ν•©λ‹ˆλ‹€.
  • n λ§€κ°œλ³€μˆ˜λŠ” λΆ„μœ„μˆ˜μ˜ 수λ₯Ό μ§€μ •ν•©λ‹ˆλ‹€.
import statistics
 
data = [1, 2, 3, 4, 5, 6, 7, 8, 9]
 
quartiles = statistics.quantiles(data, n=4)
print(quartiles)  # 좜λ ₯: [3.0, 5.0, 7.0]

πŸ“œμΆœμ²˜(μ°Έκ³  λ¬Έν—Œ)


πŸ”—μ—°κ²° λ¬Έμ„œ

Python 3.9 μ΄ν•˜