> I wish python had a clean way to define types without defining classes
dataclasses are pretty much that. They use the class mechanism but they're culturally accepted now as a clean way to build the equivalent of structs.
Re:You might not need a Python class
by paulddraper on Thursday July 24, 2025 @04:30PM
(#44675715)
^ Agreed.
And e.g. you can add dataclasses-json for JSON serializability: https://pypi.org/project/dataclasses-json/
Re:You might not need a Python class
by isjustintime on Thursday July 24, 2025 @09:15PM
(#44678384)
Yes, dataclasses are great. I often use them as a pretty clear and adopted way to handle serialization and deserialization, configs, etc.
Re:You might not need a Python class
by thenaturalist on Thursday July 24, 2025 @04:31PM
(#44675732)
> classes are a bad way to model data imo
Genuinely curious: why?
Not the one you’re replying to, but my take: The main problem with classes is that they conflate a lot of concepts that should be separate and you usually need only one or a few at once, but are 5orced into all of them. Namely:
• Product type
• Reference semantics
• Namespacing
• Pipeline-style call syntax
• Redefinition of infix operators
• Dynamic dispatch
• Subtyping
For example, you cannot have a product type with value semantics or a type that redefines infix operators but is not a product type.
Re:You might not need a Python class
by anttiharju on Thursday July 24, 2025 @04:28PM
(#44675673)
pydantic?
I wish python had a clean way to define types without defining classes. Think a _good_ mechanism to define the shape of data contained within primitives/builtins containers without classes -- ala json/typescript (ideally extended with ndarray and a sane way to represent time)
Python classes wrapped around the actual "data" are sometimes necessary but generally always bad boilerplate in my experience.