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]