by skybrian · 4y ago · 0 comments
Ideally everyone would be using a single type definition. Admittedly that's more common with protobufs, though, where you can't send any data that's not in the definition.
Come to think of that, it's true of plain old structs too.
Because your application will fail if fields you need aren't there.
Classes aren't a great abstraction when enforcing program invariants like "this object must at least have fields a and b." With a dict you can just have "dict(a=1, b=2, c=3)" and it works everywhere without serialization/deserialization and manual type conversions. Python's type checkers can't provide any safety for you if you do that, but that's a deficiency in the language, not the concept.
Can you elaborate on the functional difference between
- an enhanced dictionary where certain keys are guaranteed as part of the type, and
- a record type (class, struct, whatever) with named fields?