Dataclasses

19th Nov 2024 I was trying to write an article on Dataclasses1 and I looked up an example on the official website2 Easy enough on the face of it, but it led me into quite a rabbit hole. A dataclass allows for the creation of “special methods” for a class when the annotation @dataclass is sighted. So if I have a class “Superhero” with the annotation : @dataclass class Superhero: """ A class of superheroes and properties about their appearance in Marvel movies """ name: str superpower: str appearances: int = 0 def beckon(self): print(f"{self.name} with the super power - {self.superpower} has appeared in Marvel movies {self.appearances} times") superhero = Superhero("Aquaman","Underwater breathing",2) superhero.beckon() It effectively means that : ...

November 19, 2024 · 3 min · Abhiram R