콘솔워크

dataclass 대신 attr을 활용한 데이터 객체 클래스 만들기 본문

프로그래밍/python

dataclass 대신 attr을 활용한 데이터 객체 클래스 만들기

콘솔워크 2023. 6. 20. 07:18
반응형
import attr
from typing import List


@attr.s(kw_only=True)
class ProductAddDto:
    product_name: str = attr.ib(default="")
    price: str = attr.ib(default="")  # 상품가격
    discount_price: str = attr.ib(default="")  # 할인가격
    category_id: str = attr.ib(default="")  # 카테고리
    detail_imgs: List[int] = attr.ib(default=[])  # 상세이미지
    terms_imgs: List[str] = attr.ib(default=[])  # 약관이미지
 
if __name__ == "__main__":
    productAddDto = ProductAddDto()

    productAddDto.product_name = "상품명입니다."
    productAddDto.category_id = "카테고리입니다."
    productAddDto.detail_imgs = [
        "dimg1.jpg",
        "dimg2.png",
    ]
    productAddDto.terms_imgs = [
        "terms1.jpg",
        "terms2.png",
    ]

 

 

ProductData 모델을 작성하는 코드이다.

getter setter를 별도로 설정하지 않아도 값 할당이 가능하다.

 

 

반응형