์๋
๋ฌธ์์ ๋ฐ๋ฅด๋ฉด 3.10์ ๋นํด 10% ~ 60% ๋นจ๋ผ์ก๋ค. ์ฆ 1.25 ๋ฐฐ ๋นจ๋ผ์ก๋ค
์์ธ ์ฒ๋ฆฌ ์ ๊ตฌ๋ฌธ (except*)
์์ธ๋ฌธ์ ๋ํ ๊ทธ๋ฃน์ ์ค์ ํ ์ ์๊ณ , ํ๋ฒ์ ์ฌ๋ฌ ์์ธ๋ฅผ ๋ฐ์ ์ ์๋ค.๋ํ ์ถ๋ ฅ๋ฌธ์ ์์๊ฒ ๊ทธ๋ ค์ค๋ค
def f():
raise ExceptionGroup(
"group1",
[
OSError(1),
SystemError(2),
ExceptionGroup(
"group2",
[
OSError(3),
RecursionError(4)
]
)
]
)
try:
f()
except* OSError as e:
print("There were OSErrors")
except* SystemError as e:
print("There were SystemErrors")
There were OSErrors
There were SystemErrors
+ Exception Group Traceback (most recent call last):
| File "<stdin>", line 2, in <module>
| File "<stdin>", line 2, in f
| ExceptionGroup: group1
+-+---------------- 1 ----------------
| ExceptionGroup: group2
+-+---------------- 1 ----------------
| RecursionError: 4
+------------------------------------
# tomlib ์ถ๊ฐ
๋ฐ์ดํฐ ์ง๋ ฌํ ํฌ๋งท์ธ TOML์ ํ์ฑํ ์ ์๋ ๋ด์ฅ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์ถ๊ฐ๋์๋ค.
import tomllib
with open("pyproject.toml", "rb") as f:
data = tomllib.load(f)
์ ์ฌ ๋ฐ์ดํฐ JSON, YAML๊ณผ ๋น๊ตํ๋ฉด
ํน์ง | JSON | YAML | TOML |
ํฌ๋งท ์ข ๋ฅ | ๋ฐ์ดํฐ ๊ตํ | ์ธ๊ฐ์ด ์ฝ๊ธฐ ์ฌ์ด ๋ฐ์ดํฐ | ์ค์ ํ์ผ์ฉ |
๋ฌธ๋ฒ | ์๊ฒฉํ๊ณ ๊ฐ๊ฒฐํ | ์ฌ์ฉ์ ์นํ์ ์ด๊ณ ์ ์ฐํ | ํค-๊ฐ ์ |
์ฃผ์ ์ฒ๋ฆฌ | ์ง์ํ์ง ์์ | ์ง์ | ์ง์ |
๋ฐ์ดํฐ ํ์ | ๊ธฐ๋ณธ์ ์ธ ํ์ (๋ฌธ์์ด, ์ซ์, ๊ฐ์ฒด, ๋ฐฐ์ด, ๋ถ๋ฆฌ์ธ, ๋) | ํ๋ถํ ํ์ (JSON ํ์ ์ง์ ๋ฐ ์ด์ง ๋ฐ์ดํฐ ๋ฑ) | ํ๋ถํ ํ์ (๊ธฐ๋ณธ ํ์ ๋ฐ ๋ ์ง ์ง์) |
๊ฐ๋ ์ฑ | ์ค๊ดํธ์ ๋ฐ์ดํ ๋๋ฌธ์ ๊ฐ๋ ์ฑ์ด ๋จ์ด์ง | ์ต์ํ์ ๊ตฌ๋์ ์ผ๋ก ๋งค์ฐ ๊ฐ๋ ์ฑ์ด ์ข์ | ๋ช ํํ ๊ตฌ์กฐ๋ก ๊ฐ๋ ์ฑ์ด ์ข์ |
์์ฑ ์ฉ์ด์ฑ | ์๊ฒฉํ ๋ฌธ๋ฒ์ผ๋ก ์ธํด ์์ฑ์ด ๋ ํธ๋ฆฌํจ | ๊ฐ๊ฒฐํ๊ณ ์ฌ์ | ์ง๊ด์ ์ด๊ณ ์ฌ์ |
ํ์ผ ํ์ฅ์ | .json | .yaml ๋๋ .yml | .toml |
์ฌ์ฉ์ฒ | ์น API, ํ๋ ์น ์ ํ๋ฆฌ์ผ์ด์ ์ ์ค์ | ์ค์ ํ์ผ, ๋ฐ์ดํฐ ์ง๋ ฌํ | ์ค์ ํ์ผ, Rust ํ๊ฒฝ์์ ๋ ์ผ๋ฐ์ |
์๋ฌ ๋ฌธ๊ตฌ ๊ฐ์
์๋ฌ๊ฐ ๋ฐ์ํ ๋ถ๋ถ์ ๋ฐ์ค๋ก ์๋ ค์ค๋ค.
์ฌ์ฉ์ ์ ์ฅ์์ ๊ฐ์ฅ ํฌ๊ฒ ๋ค๊ฐ์๋ค.
x ๋ณ์์ ๋ํ NoneType์์ ์ด๋ค ๋ณ์์ x๋ฅผ ์กฐํํ๋ค๊ฐ ์๋ฌ๊ฐ ๋ฐ์ํ๋์ง, ๋ฐ์ค๋ก ์๋ ค์ค๋ค.
Traceback (most recent call last):
File "distance.py", line 11, in <module>
print(manhattan_distance(p1, p2))
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "distance.py", line 6, in manhattan_distance
return abs(point_1.x - point_2.x) + abs(point_1.y - point_2.y)
^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'x'
์์ฌ์ด ๊ฑด ์๋ ์ฝ๋์์
์ ๋ก ๋๋ฐ์ด๋์ ๋ํด ์ฐ์ฐ์ ๊ธฐ์ค์ผ๋ก ์๋ ค์ค๋ค. (๋ณ์ ๊ธฐ์ค์ด์๋ค๋ฉด)
raceback (most recent call last):
File "calculation.py", line 54, in <module>
result = (x / y / z) * (a / b / c)
~~~~~~^~~
ZeroDivisionError: division by zero
Variadic ์ ๋๋ฆฌ
TypeDict๋ฅผ ์์๋ฐ์ผ๋ฉดJava - Spring ์์์ ๋ชจ๋ธ์ด๋ objectMapper.readValue ์ฒ๋ผ, ํด๋์ค-๊ฐ์ฒด์ ํ๋กํผํฐ์ ๊ฐ์ ๋ฃ์ด์ค๋ค.
class Movie(TypedDict):
title: str
year: NotRequired[int]
m1: Movie = {"title": "Black Panther", "year": 2018} # OK
m2: Movie = {"title": "Star Wars"} # OK (year is not required)
m3: Movie = {"year": 2022} # ERROR (missing required field title)
NotRequired ์ค์ ๋ ๊ฐ๋ฅํ๋ค
class Movie(TypedDict, total=False):
title: Required[str]
year: int
Self Return ์ฌ์ฉ
๊ธฐ์กด์๋ self๋ฅผ ๋ฐํํ๋ฉด ํ์ ํํธ๋ฅผ ์ฌ์ฉํ์ฌ ํด๋น ํ์ ์ ๋ฐํํ ์ ์์๋ค.
class Shape:
def set_scale(self, scale: float) -> Shape:
self.scale = scale
return self
Shape().set_scale(0.5) # => Shape
ํ์ง๋ง ์ด๋ฐ ๊ฒฝ์ฐ, Circle().set_scale(0.5) ์์ Shape ํํ๋ก ๋ฐํ๋์ด, set_radius()๋ฅผ ํธ์ถ ํ ์ ์๊ฒ ๋๋ค.
class Circle(Shape):
def set_radius(self, r: float) -> Circle:
self.radius = r
return self
Circle().set_scale(0.5) # *Shape*, not Circle
Circle().set_scale(0.5).set_radius(2.7)
# => Error: Shape has no attribute set_radius
๊ธฐ์กด ๋ฒ์ ์์๋ ์๋์ ๊ฐ์ด ๋ณ๋์ TShap๋ฅผ ์ฌ์ฉํด์ผ ํ์ง๋ง
from typing import TypeVar
TShape = TypeVar("TShape", bound="Shape")
class Shape:
def set_scale(self: TShape, scale: float) -> TShape:
self.scale = scale
return self
class Circle(Shape):
def set_radius(self, radius: float) -> Circle:
self.radius = radius
return self
Circle().set_scale(0.5).set_radius(2.7) # => Circle
Self ๋ผ๋ ์ง๊ด์ ์ธ ํํ๋ฅผ ์ ๊ณตํ์ฌ ๊ฐ์ ํ์๋ค.
from typing import Self
class Shape:
def set_scale(self, scale: float) -> Self:
self.scale = scale
return self
class Circle(Shape):
def set_radius(self, radius: float) -> Self:
self.radius = radius
return self
์ฌ์ฉํ ์ฝ๋๋ ๋ชจ๋ PEP ๊ณต์ ๋ฌธ์๋ฅผ ์ฐธ์กฐํ์ต๋๋ค.
'๐ Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Python] GIL์ด๋? ํ์ด์ฌ์์์ ๋ฉํฐ์ฐ๋ ๋ (0) | 2024.05.06 |
---|---|
[Python] Pandas, Numpy ์ฑ๋ฅ ํฅ์ (feat.Pandas vs Numpy) (0) | 2024.05.06 |
์๋ฃ๊ตฌ์กฐ - List, Dict, Set, Tuple (0) | 2022.10.27 |
[Python] sort, sorted ( Tim sort, ํ ์ํธ) (0) | 2022.10.23 |
ํ์ด์ฌ Python GUI - ๊ทธ๋ํ (pyqtgraph, matplotlib) (realtime, ์ค์๊ฐ) (0) | 2021.02.20 |