Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ghost opened this issue Jul 15, 2023 · 2 comments
Closed

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

ghost opened this issue Jul 15, 2023 · 2 comments
Labels
docs Documentation in the Doc dir

Comments

@ghost
Copy link

ghost commented Jul 15, 2023

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

@ghost ghost added the docs Documentation in the Doc dir label Jul 15, 2023
@terryjreedy
Copy link
Member

That Point definition is in a previous example. The paragraph that follows explains the solution for matching positional parameters. In this case, add __match_args__ = ('x', 'y') to Point and the example works. I will augment the Point list example with a proper Point definition. Working on PR.

terryjreedy added a commit to terryjreedy/cpython that referenced this issue Jul 15, 2023
Add Point definition with this attribute before example
that needs it.
@ghost
Copy link
Author

ghost commented Jul 16, 2023 via email

terryjreedy added a commit that referenced this issue Jul 17, 2023
Add Point definition with this attribute before example
that needs it.
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jul 17, 2023
)

Add Point definition with this attribute before example
that needs it.
(cherry picked from commit 7aa89e5)

Co-authored-by: Terry Jan Reedy <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jul 17, 2023
)

Add Point definition with this attribute before example
that needs it.
(cherry picked from commit 7aa89e5)

Co-authored-by: Terry Jan Reedy <[email protected]>
terryjreedy added a commit that referenced this issue Jul 17, 2023
…106820)

Add Point definition with this attribute before example
that needs it.
(cherry picked from commit 7aa89e5)

Co-authored-by: Terry Jan Reedy <[email protected]>
terryjreedy added a commit that referenced this issue Jul 17, 2023
…106819)

Add Point definition with this attribute before example
that needs it.
(cherry picked from commit 7aa89e5)

Co-authored-by: Terry Jan Reedy <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir
Projects
None yet
Development

No branches or pull requests

2 participants
@terryjreedy and others