3 ms·
> Don't let dicts spoil your code (2020) (roman.pt) * Conditions applies * Apply only for when parsing I/O. Do not substitute primitives with classes inside yo
by BiteCode_dev 4y ago
> Don't let dicts spoil your code (2020) (roman.pt) * Conditions applies
* Apply only for when parsing I/O. Do not substitute primitives with classes inside your code base for no good reason. Unless validation is needed, prefer a NamedTuple.
- Hackbraten 4y agoOther than validation, I can imagine several good reasons why one might want to wrap a primitive inside a class. For example, you may have a function: group_by_age() -> Dict[int, List[str]] which might be perfectly good for your use case, but I can see why one might instead prefer: group_by_age() -> Dict[Age, List[CustomerId]] for self-documentation and expressiveness. Your test assertions may also become easier to read: assert group_by_age() == { Age(23): [ CustomerId("0471"), CustomerId("3390"), ], Age(42): [ CustomerId("2334"), ], }
- BiteCode_dev 4y agoUse NewType for this: https://mypy.readthedocs.io/en/stable/more_types.html?highlight=id#newtypes https://mypy.readthedocs.io/en/stable/more_types.html?highli...