vimwiki

Python dataclasses

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

parameter

frozen

Change field to read only.

@dataclass(frozen=True)
class Fruit:
  name: str
  color: str

slots (py 3.10+)

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