Skip to content

docs.python.org | 4.6. match Statements #106780

Closed
@ghost

Description

Documentation

https://docs.python.org/3/tutorial/controlflow.html

I fixed the code as follows since points is not defined, still it does not work:

class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

points = [Point(0,7), Point(0,9)]

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

error:

Traceback (most recent call last):
  File "c:\Users\moham\OneDrive\Education\UDST\gpaCal.py", line 15, in <module>
    case [Point(0, y1), Point(0, y2)]:
          ^^^^^^^^^^^^
TypeError: Point() accepts 0 positional sub-patterns (2 given)

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dir

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions