3 ms·
I enjoy novel ideas, they are fun to entertain, but this is quite spicy of a take. I kind of understand where OP is coming from, but if you start to understand
by kortex 4y ago
I enjoy novel ideas, they are fun to entertain, but this is quite spicy of a take. I kind of understand where OP is coming from, but if you start to understand python internals, you would quickly see why it's completely untenable.
data Point3D(x, y, z) from Vector:
is never, ever, ever, ever gonna happen. It's ugly, alien to the rest of python style, will be a massive pain to implement in the lexer/parser, adds another keyword and mechanism, and most importantly, makes the word "data" unusable as a variable name. Dataclasses are "just" decorator functions (there is some special optimization and meta magic under the hood, but still just a callable).
There's a glaring reason why we can't "just make all classes dataclasses" - it would break the entire data model of the language. "class" keyword is basically syntactic sugar for the "type" builtin. Everything (with a few small exceptions) you touch in python is a subclass of object. Changing the behavior of class to by default behave like dataclass would have knock-on effects across the entire language on every single python3 codebase on the planet.
Ok, even assuming we could surmount the insurmountable breaking changes, would we even want default classes to be dataclasses? Not in the slightest. Python may not have true private variables, but libraries/APIs leverage dunders, properties, and hidden state to do all kinds of OOPy things. You can't do that with dataclasses. Many times you have objects bound to attributes which don't have concrete types, such as endpoints in HTTP frameworks and anything with dynamic dispatch (yeah there's Callable, but it is tricky to type check).
Dataclass also has a self-documentation effect: it says "this thing is kind of like a record". Not a primitive, not a function, not a router, view, thread, tree node, god-class or stack frame. If I see dataclass, I almost always expect to be able to de/serialize it to some sort of message. Complex state, anything unpickleable, or exotic types don't really belong in a dataclass.
- kortex 4y agoOff the top of my head, python classes which shouldn't/can't be dataclasses: The entire modules typing, os, sys, struct, inspect, json. Anything which is a metaclass or relies on metaclass behavior (you have to be careful combining dataclass with other metaclasses, it's possible but with footguns akimbo). Datetime actually should be a dataclass, but that api is so crufty and unintuitive that it's never gonna happen, and trying to shoehorn it to a dataclass would break oodles of libraries and applications. threading: Thread, Lock, Rlock anything whichs uses locks Numpy.ndarry, pandas.Series, pandas.Dataframe, torch.Tensor. Numerous scikit-learn models. Probably the vast majority of c extensions. pydantic.BaseModel, fastapi.FastAPI, fastapi.APIRouter (same with flask/django), celery.Task. I could go on but I think I've made my point. There are plenty of non-dataclass use cases out there.