Skip to content

Instantly share code, notes, and snippets.

@mypy-play
Created March 8, 2026 22:49
Show Gist options
  • Select an option

  • Save mypy-play/58f33d5e8d910acf048e474c133162bc to your computer and use it in GitHub Desktop.

Select an option

Save mypy-play/58f33d5e8d910acf048e474c133162bc to your computer and use it in GitHub Desktop.
Shared via mypy Playground
from typing import Iterable, Protocol
from dataclasses import dataclass
@dataclass
class Square:
sidelength: float
def area(self) -> float:
return self.sidelength * self.sidelength
@dataclass
class Rectangle:
wd: float
ht: float
def area(self) -> float:
return self.wd * self.ht
class HasArea(Protocol):
def area(self) -> float: ...
def sum_area(shapes: Iterable[HasArea]) -> float:
return sum(s.area() for s in shapes)
print(sum_area([Rectangle(3, 4), Square(1)]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment