0

I want to apply a match case syntax to a list of points to tuples as in the example below:

match points:
    case []:
        print("No points")
    case [Point(0, 0)]:
        print("The origin")
    case [Point(x, y)]:
        print(f"Single point {x}, {y}")
    case [Point(0, y1), Point(0, y2)]:
        print(f"Two on the Y axis at {y1}, {y2}")
    case _:
        print("Something else")

This what i am currently doing:

class Point:
    x:int
    y:int
pointer = Point()
pointer2=Point()
pointer.x = 0
pointer.y=2
pointer2.x=0
pointer2.y=2
points =[pointer,pointer2]
match points:
    case []:
        print("No points")
    case [Point(0,0)]:
        print("The origin")
    case [Point(x,y)]:
        print("Only a single point, X={}, Y={}".format(x,y))
    case [Point(0,y),Point(0,y2)]:
        print("Two on Y axis, Y1={}, Y2={}".format(y1,y2))
    case _:
        print("Something else")

but i get this error:

TypeError Traceback (most recent call last) Cell In[56], line 11 9 case [Point(x,y)]: 10 print("Only a single point, X={}, Y={}".format(x,y)) 11 case [Point(0,y),Point(0,y2)]: 12 print("Two on Y axis, Y1={}, Y2={}".format(y1,y2)) 13 case _:

TypeError: Point() accepts 0 positional sub-patterns (2 given)

How can i make this code work, and get the right output?

  • the article linked solved my problem - https://stackoverflow.com/questions/69627609/point-accepts-0-positional-sub-patterns-2-given – Daniel Ayomide AMOO Jul 06 '23 at 15:00
  • for my code I changed my class declaration to - `class Point: __match_args__=("x", "y") x:int y:int ` and changed the 4th case to - `case [Point(0,y),Point(0,y2)]: print("Two on Y axis, Y1={}, Y2={}".format(y,y2))` – Daniel Ayomide AMOO Jul 06 '23 at 15:03

0 Answers0