from dataclasses import dataclass
class Fruit:
name: str
color: str
calories: int = 10
apple = Fruit('apple', 'green')
apple.color = red
print(apple) # print work by default
Change field to read only.
@dataclass(frozen=True)
class Fruit:
name: str
color: str
Create a table with the fields, with the benefit of faster access and memory saving.
@dataclass(frozen=True, slots=True)
class Fruit:
name: str
color: str