Created
March 8, 2026 22:49
-
-
Save mypy-play/58f33d5e8d910acf048e474c133162bc to your computer and use it in GitHub Desktop.
Shared via mypy Playground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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