2

The full source code is from here.

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")

How could I run the above code to obtain each case match?

I tried these:

>>> where_is((1,0))
Not a point

>>> where_is((Point))
Not a point
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
  • You probably need to lookup for the term "type annotation" in python. – Abdul Niyas P M Dec 28 '22 at 04:08
  • 1
    Please [edit] to limit your post to only 1 question. Your 1st question about `:` in the class variables is already answered by [What are variable annotations?](https://stackoverflow.com/questions/39971929/what-are-variable-annotations) which can lead your question to be closed as a duplicate. Your 2nd question about matching on specific instances of a custom class is probably the more useful one as that has no obvious duplicates. – Gino Mempin Dec 28 '22 at 04:17
  • For the future, try to avoid using "this" in question titles. A question where someone needs to click through and read the body before they know what you're asking is basically clickbait. – Charles Duffy Dec 28 '22 at 04:18
  • 1
    Thanks for the input and feedback! I have edited my post according to suggestions given. I have looked into some of the links and answer provided, it does clarify me on type hint and how it operates. – Yap Wei Cheng Dec 28 '22 at 19:35
  • I had found the solution for my question, basically there is an error in the source code. I will provide the answer and solution given by others from python forum here [link](https://python-forum.io/thread-39059.html). – Yap Wei Cheng Dec 31 '22 at 13:57

1 Answers1

-1

In this case, the colon ":" is used to specify a type hint for the variables "x" and "y" which are public member variables in that class. They are set when declaring instances of Point() in your examples.

This explains more about type hinting: https://peps.python.org/pep-0484/

ScottJ
  • 19
  • 3