I've been learning about dataclasses, and was reworking an old project trying to integrate a dataclass into the program in place of a dictionary system I was using. The code blocks below are essentially the respective new and old methods being used to build a dataframe of several thousand items. My problem is I don't understand the use-case for the dataclass over a dictionary.
What I want to know is:
When should I use a dataclass over a dictionary (or vice versa)?
Programmatically, in this instance of simply cataloguing data, is either method more efficient/optimized than the other?
In actual practice is either method encouraged over the other (for reasons of efficiency, readibility, industrial standards, or otherwise)?
Method using @dataclass
@dataclass
class Car:
year: int = None
model: str = None
def main():
foo = {}
for name in car_list:
bar = Car()
bar.year = get_year(name)
bar.model = get_model(name)
foo[name] = vars(bar)
df = pd.DataFrame.from_dict(foo)
Method using Dictionary
def main():
foo = {}
for name in car_list:
bar = {
'year': None
'model': None
}
bar['year'] = get_year(name)
bar['model'] = get_model(name)
foo[name] = bar
df = pd.DataFrame.from_dict(foo)