#πŸ”’ Is it better to use dataclass or a custom class for an object that holds data?

12 messages Β· Page 1 of 1 (latest)

ruby heron
#

Originally I was going to create a dataclass for an object that simply holds data:

@dataclass
class Album():
  artists: list
  songs: list
  release_date: str 
  popularity_rating: float

However I've realised I need to clean some of these attributes. E.g. the artists data is actually html that I need to parse before assigning to the artists attribute.

Would it better to just create a normal class?

I have a factory function that's creating the Album instances but I think it will be too much work for it to also parse the artists HTML that eventually becomes the artists attribute.

Thanks!

fringe pecanBOT
#

@ruby heron

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

fathom pollen
ruby heron
fathom pollen
#

Well the dataclass could still just edit it

ruby heron
#

Yeah i'm thinking I'll just use __post_init__

fathom pollen
#

Technically you probably should have your initial process parse the html before putting it in the object.

#

But if it does take way too long to do all that you can definitely just put it in the class

ruby heron
#

Yeah cool. Thanks!

#

!close

fringe pecanBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.