In Python,
I am using a dataclass named "MyDataClass" to store data returned by a http response. let's say the response content is a json like this and I need only the first two fields:
{
"name": "Test1",
"duration": 4321,
"dont_care": "some_data",
"dont_need": "some_more_data"
}
and now I have two options:
Option 1
resp: dict = The response's content as json
my_data_class: MyDataClass(name=resp['name'], duration=resp['duration'])
where I take advantage of the dataclass' automatically defined init method
or
Option 2
resp: dict = The response's content as json
my_data_class: MyDataClass(resp)
and leave the processing to the dataclass init method, like this:
def _ _ init _ _(self, resp: Response) -> None:
self.name: str = resp['name']
self.duration: int = resp['duration']
I prefer the 2nd option, but I would like to know if there is a right way to this.
Thanks.