-
dataclass: sortpython/응용 2023. 7. 10. 11:40
코드
from dataclasses import dataclass, field @dataclass(order=True) class Person: sort_index: int = field(init=False, repr=False) name: str age: int def __post_init__(self): self.sort_index = self.age person1 = Person("Garalt", 30) person2 = Person("Yennefer", 25) print(person1 > person2) print(person1)
결과
True Person(name='Garalt', age=30)
주의
- sort_index 필드가 가장 맨 처음으로 와야 한다. (dataclass 는 기본적으로 필드 항목 순으로 정렬하는 듯?)
- sort_index 에서 repr=False 설정으로 print(person1) 을 했을 때 sort_index 항목은 출력되지 않는다.
'python > 응용' 카테고리의 다른 글
functools.cache (0) 2023.07.13 pydantic: dictionary 를 object 로 변환 / json 출력 (0) 2023.07.10 dataclass: mutable default values (0) 2023.07.09 functools.partial() 활용 (0) 2023.07.07 Currying (0) 2023.06.11