Dec-28-2022, 06:23 AM
I honestly have no idea what they are talking about in the link. A dataclass uses colons like that.
from dataclasses import dataclass @dataclass class Point: x: int y: int x = Point(1, 2) print(x)
Output:Point(x=1, y=2)
But the code in the link does not use a dataclass decorator when declaring class Point. The same kind of notation is used by pydantic.from pydantic import BaseModel class Point(BaseModel): x: int y: int x = Point(x=1, y=2) print(x)
Output:x=1 y=2
Making Point a dataclass allows testing the where_is() function like this:from dataclasses import dataclass @dataclass class Point: x: int y: int def where_is(point): match point: case Point(x=0, y=0): print("Origin") case Point(x=0, y=y): print(f"Y={y}") case Point(x=x, y=0): print(f"X={x}") case Point(): print("Somewhere else") case _: print("Not a point") where_is(Point(0, 0)) where_is(Point(0, 2)) where_is(Point(2, 0)) where_is(Point(None, None)) where_is((0, 0))
Output:Origin
Y=2
X=2
Somewhere else
Not a point