4 ms·
Probably a controversial opinion, but I would like `self` removed, and have the class body be an init function. Something like: ``` dataclass NormalizedPoint(
by saiojd 4y ago
Probably a controversial opinion, but I would like `self` removed, and have the class body be an init function. Something like:
```
dataclass NormalizedPoint(x: float, y: float):
_norm = math.sqrt(x\*2 + y\*2)
x /= _norm
y /= _norm
def __add__(other: NormalizedPoint):
return NormalizedPoint(x + other.x, y + other.y)
```
In other words, I would really like some construct which sticks as close as possible to a normal function, but that also returns a (typed) object with everything in its scope.
- dragonwriter 4y ago> Probably a controversial opinion, but I would like `self` removed, and have the class body be an init function. That would be weird in classes that override the constructor (__new__), which is what is actually called (not __init__, except contingent on the result of the constructor) when the class is used as a callable. Not to mention the need to define methods in the class body, and be able to distinguish them from local functions in the constructor/initializer, which is a pretty good argument against the class body being the constructor or initializer.
- saiojd 4y agoI agree that in the general case it does not work, but if we were to introduce an alternate dataclass/class definition syntax, I feel like something lightweight like this would be nice. (Local functions could just be prefixed with underscores, no need to distinguish them from private methods.)