Convert a dataclass to a non-dataclass

This tool uses to demonstrate what dataclasses really do for us. Python's dataclass decorator gives us __init__, __repr__, and __eq__ methods and (depending on what options we send) much more as well!

Paste your dataclass below to see what happens.

Or play around: remove frozen=True, add kw_only=True, add = field(repr=False, default=0) to the last line, or try something complex.

Curious how this works? Read how and why I used match-case while writing this and the full walk through of how this works.

URL copied

Paste your dataclasses here

This is the non-dataclass version

class Point:
    __slots__ = ('x', 'y', 'z')
    __match_args__ = ('x', 'y', 'z')

    def __init__(self, x: float, y: float, z: float) -> None:
        object.__setattr__(self, 'x', x)
        object.__setattr__(self, 'y', y)
        object.__setattr__(self, 'z', z)

    def __repr__(self):
        cls = type(self).__name__
        return f'{cls}(x={self.x!r}, y={self.y!r}, z={self.z!r})'

    def __eq__(self, other):
        if not isinstance(other, Point):
            return NotImplemented
        return (self.x, self.y, self.z) == (other.x, other.y, other.z)

    def __hash__(self):
        return hash((self.x, self.y, self.z))

    def __setattr__(self, name, value):
        raise AttributeError(f"Can't set attribute {name!r}")

    def __delattr__(self, name):
        raise AttributeError(f"Can't delete attribute {name!r}")

    def __getstate__(self):
        return (self.x, self.y, self.z)

    def __setstate__(self, state):
        fields = ('x', 'y', 'z')
        for field, value in zip(fields, state):
            object.__setattr__(self, field, value)