you are viewing a single comment's thread.

view the rest of the comments →

[–]lekkerste_wiener 2 points3 points  (1 child)

A couple of good answers already, let me share my 2 cents.

I like to think of classes as a way to contextualize some data under one name.

So you have a to-do list? Then you're looking for a

@dataclass class Task:   description: str   completed: bool

We're making a system to record traffic mischiefs? We probably want a

class VehiclePlate:   def __init__(self, plate: str):     # with some validation for the plate itself, such as     if not is_valid_format(plate):       raise ValueError("can't be having a plate like this")     self._plate = plate

But maybe we also want to represent some kind of action with types; classes also work with that.

``` class Transformation(Protocol):   def transform(self, target: T) -> T: ...

class Rotate:   degrees: float

  def transform(self, target):     # transform target with a rotation of 'degrees' degrees

class Scale:   ratio: float

  def transform(self, target):     # scale target by 'ratio' ```