Dec-28-2022, 10:41 AM
(This post was last modified: Dec-28-2022, 10:41 AM by deanhystad.)
I guess it can be type notation for a class variable. Syntactically that is correct. Logically it doesn't make any sense.
So we have our class Point, a singleton that requires we make instances so they can be passed to where_is() to demonstrate how match works.
It works better, but is longer, if x and y are properties.
So we have our class Point, a singleton that requires we make instances so they can be passed to where_is() to demonstrate how match works.
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") point = Point() where_is(point) Point.x, Point.y = 0, 0 where_is(point) Point.x, Point.y = 0, 1 where_is(point) Point.x, Point.y = 1, 0 where_is(point) where_is((0, 0))That is just ugly. If the ": int" really is type annotation for class variables, this is a bad example.
It works better, but is longer, if x and y are properties.
class Point(): def __init__(self, x=None, y=None): self._x, self._y = x, y @property def x(self) -> int: return self._x @x.setter def x(self, value: int): self._x = value @property def y(self) -> int: return self._y @y.setter def y(self, value: int) : self._y = value 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, 1)) where_is(Point(1, 0)) where_is(Point()) where_is((0, 0))