2 ms·
Statically Typed Functional Programming with Python 3.12
- deleted 2y ago[deleted]
- owickstrom 2y agoAuthor here! Happy to discuss this topic.
- okso 2y agoYou may be interested in the type matching provided by Pydantic. It is a handy way to avoid writing `match-case` expressions. ``` >>> from pydantic import BaseModel >>> from typing import Literal, List >>> class Chicken(BaseModel): ... n_legs: Literal[2] >>> class Cat(BaseModel): ... n_legs: Literal[4] >>> class Farm(BaseModel): ... animals: List[Chicken | Cat] >>> Farm.model_validate(dict(animals=[{'n_legs': 2}, {'n_legs': 4}])) Farm(animals=[Chicken(n_legs=2), Cat(n_legs=4)]) ```