Join us
python = โkingโ ๐
Letโs start Data Classes ๐that uses dataclasses module
In a normal concrete class, we want to define a constructor and __repr__ to make the custom representation of the object when object is passed as parameter to print() function.
Am I only the one who hates writing long constructor method in the class ? Am I only the one who hates the memory representation of the object when i print the object as print(โmy-objectโ) ?
If you guys feels my pain! Cheers dude , You are in the right place ! ๐
Letโs start Data Classes ๐that uses dataclasses module
In a normal concrete class, we want to define a constructor and __repr__ to make the custom representation of the object when object is passed as parameter to print() function. Look at the code below !
The above code gives output as,
Do you think the above code is more complex to write and understand ??
Letโs see the same type of code using Data class
output of the above code :
Do you see the output, it is so simple . Dataclasses uses a constructor internally and it has itโs own custom __repr__ method.
Another important feature of dataclass is 2 objects of a dataclass with same content in them always gives โTrueโ on โ==โ operator. While Normal concrete class objects gives โFalseโ
Letโs assume that i have 2 objects each in both dataclass and normal concrete class with same data as,
If I use โ==โ operator on objects of their respective classes .Can you guess what it will return ? โTrueโ / โFalseโ
output of the above code :
Dataclass objects with same data returns โTrueโ on โ==โ operator and Concrete class objects with same data returns โFalseโ on โ==โ operator.
Follow , clap and share if you find this blog helpful !
DataClasses part-2 will post soonโฆโฆโฆ๐
Join other developers and claim your FAUN account now!
Backend developer, Clarivate
@yokee-ss-hhInfluence
Total Hits
Posts
Only registered users can post comments. Please, login or signup.