I have the following dataclass:
@dataclass
class Image:content_type: strdata: bytes = b''id: str = ""upload_date: datetime = Nonesize: int = 0def to_dict(self) -> Dict[str, Any]:result = {}if self.id:result['id'] = self.idif self.content_type:result['content_type'] = self.content_typeif self.size:result['size'] = self.sizeif self.upload_date:result['upload_date'] = self.upload_date.isoformat()return result
Is there any way to simplify to_dict
method? I don't want to list all of the fields using if
.