Created
September 13, 2022 09:32
-
-
Save tempKDW/08e576a5c93e3bc438049f12782cb671 to your computer and use it in GitHub Desktop.
frozen dataclass 에 db 를 주입받아 사용
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@dataclass(frozen=True) | |
class Bistro: | |
id: Optional[int] | |
cuisine: Cuisine | |
name: str | |
location_url: str | |
def create(self, db: BistroDB): | |
if self.id: | |
raise ValueError(f"{self} 는 이미 db 에 존재합니다.") | |
obj: BistroDB = db.create(self.cuisine.value, self.name, self.location_url) | |
return Bistro(id=obj.id, cuisine=Cuisine(obj.cuisine_value), name=obj.name, location_url=obj.location_url) | |
def __repr__(self): | |
return f"{self.cuisine} {self.name}({self.id}) : {self.location_url}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
이 경우 사용자는
Bistro(...).create(db)
와 같은 형식으로 객체를 만들어주어야 한다.frozen을 유지한 채로 생성자만으로 객체를 만들수 없나?