Skip to content

Instantly share code, notes, and snippets.

@tempKDW
Created September 13, 2022 09:32
Show Gist options
  • Save tempKDW/08e576a5c93e3bc438049f12782cb671 to your computer and use it in GitHub Desktop.
Save tempKDW/08e576a5c93e3bc438049f12782cb671 to your computer and use it in GitHub Desktop.
frozen dataclass 에 db 를 주입받아 사용
@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}"
@tempKDW
Copy link
Author

tempKDW commented Sep 13, 2022

이 경우 사용자는 Bistro(...).create(db) 와 같은 형식으로 객체를 만들어주어야 한다.
frozen을 유지한 채로 생성자만으로 객체를 만들수 없나?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment