python3.7 이후) dataclass __post_init__ 간략하게 알아보기

2021. 4. 25. 10:55분석 Python/구현 및 자료

728x90

처음 보는 거라 처음에 굉장히 당황해서 남겨 놓는다.

 

python3.7 부터 생긴 기능인 것 같은데 

dataclass decorator와 __post_init__을 사용해서 디폴트값을 설정할 수 가 있고, overidding을 계속 안해줘도 되는 것 같다.

 

디버깅을 해도 이부분은 확인이 안되서 난감했는데, 좋은 기능이라 생각하고 메모 

from dataclasses import dataclass, field

@dataclass
class Book :
    n_d: int = 8
    n_a: int = 8
    n_steps: int = 3

    def __post_init__(self,):
        self.full = self.n_d + self.n_a

c = Book()
print(c.n_d)
print(c.full)

c = Book(n_d=10,n_a=20)
print(c.n_d)
print(c.full)
728x90