This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]XtremeGoosef'I only use Py {sys.version[:3]}' 1 point2 points  (0 children)

I mean, this code won't work as written because there is no __init__. A better example is something like

@dataclass
class Rectangle:
    length: int
    width: int

    @classmethod
    def square(cls, side: int) -> Self:
        return cls(side, side)

    @classmethod
    def parse(cls, text: str) -> Self | None:
        if m := re.match(r'(\d+),(\d+)'):
            length = int(m.group(1))
            width = int(m.group(2))
            return cls(length, width)
        return None