Classes are blueprints of objects that contain methods and data (attributes).
Bare-bone classes don't provide a lot of useful functionality for data-oriented classes. From Python 3.7, we have a dataclass module.
Here are some powerful features of Data classes:
built-in init (quickly fill an object with data)
easy ways to print, compare & order data
# regular python class
class Person:
def __init__(self, name, surname, age):
self.name = name
self.surname = surname
self.age = age
# 2 objects of same init args
p1 = Person(Suraj, Pandiaraj, 29)
p2 = Person(Suraj, Pandiaraj, 29)
id(p1) # 449539696
id(p2) # 439884857
p1 # <__main__.Person object at 0x1002939>
p1 == p2 # False
1️⃣ Data classes will make things easier for us to see what's in the object as seen below.
from dataclasses import dataclass
@dataclass
class Person:
name: str
surname: str
age: int
p1 = Person(Suraj, Pandiaraj, 29)
p2 = Person(Suraj, Pandiaraj, 29)
id(p1) # 449539696
id(p2) # 439884857
p1 # Person(name='Suraj', surname='Pandiaraj', age=29)
p1 == p2 # True
2️⃣ We can also set order=True
to compare 2 objects:
from dataclasses import dataclass, field
@dataclass(order=True)
class Person:
sort_index: int = field(init=False, repr=False)
name: str
surname: str
age: int
def __post_init__(self): # called after init
self.sort_index = self.age
p1 = Person(Suraj, Pandiaraj, 29)
p2 = Person(Nabil, Goat, 27)
p1 > p2 # True
3️⃣ Default values in data classes:
from dataclasses import dataclass, field
@dataclass(order=True)
class Person:
sort_index: int = field(init=False, repr=False)
name: str
surname: str
age: int
strength: int = 100
def __post_init__(self):
self.sort_index = self.age
p1 = Person(Suraj, Pandiaraj, 29)
p2 = Person(Nabil, Goat, 27, 89)
p1 # Person(name='Suraj', surname='Pandiaraj', age=29, strength=100)
p2 # Person(name='Nabil', surname='Goat', age=27, strength=89)
4️⃣ Freezing with data classes (read-only objects):
from dataclasses import dataclass, field
@dataclass(order=True, frozen=True)
class Person:
sort_index: int = field(init=False, repr=False)
name: str
surname: str
age: int
strength: int = 100
def __post_init__(self):
object.__setattr__(self, 'sort_index', self.age)
5️⃣ Print out data nicely with data classes:
from dataclasses import dataclass, field
@dataclass(order=True)
class Person:
sort_index: int = field(init=False, repr=False)
name: str
surname: str
age: int
strength: int = 100
def __post_init__(self):
self.sort_index = self.age
def __str__(self):
return f'{self.name}, {self.surname} ({self.age})'
p2 = Person(Nabil, Goat, 27, 89)
p2 # Nabil, Goat (27)
In a nutshell, Dataclasses allow us to quickly create objects that is data-intensive in an easy way.