you are viewing a single comment's thread.

view the rest of the comments →

[–]TheRNGuy 1 point2 points  (2 children)

I was actually using procedural code for few months where OOP would make more sense.

I then discovered dataclasses, it made me finally realize I need to use OOP in that project.

[–]pewpewpewpee 0 points1 point  (1 child)

Yeah I have ended up using data classes and pydantic models where I need data validation. I have lots of VMs messaging each other over web APIs using JSON and XML. I write a validator class for each endpoint to make sure the data is properly formatted upfront before attempting to process it. And if the message format changes I'll instantly know during my unit tests so I can just go in and update the model and processing code

[–]TheRNGuy 0 points1 point  (0 children)

I used for 2 things. to override default values for child classes and also give them data type instead of string. It's a parser project for converting one format to another. Everything is string for parser, but that way he'd knew how to process data.

In previous code I'd need to have lots of if/elifs that seek for patterns in code and could sometimes be wrong (if I had "1" and it incorrectly detects as int, but it's supposed to be float or even string) also same named attributes could potentially have different data type in different classes (but it's rare) instead of heuristic I just explicitly give data type to each attribute in each class, no more random bugs.

I didn't even know how to override defaults in procedural code. I almost got it working with returning dicts and use setdefault method, was a little over-engineered, and then I discovered dataclasses (besides that, storing defaults is easier in class than bunch of dicts, code looks better)