Skip to content
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

gh-106780: Add __match_args__ to tutorial example #106784

Merged
merged 3 commits into from
Jul 17, 2023

Conversation

terryjreedy
Copy link
Member

@terryjreedy terryjreedy commented Jul 15, 2023

Add Point definition with this attribute before example that needs it.


📚 Documentation preview 📚: https://cpython-previews--106784.org.readthedocs.build/

Add Point definition with this attribute before example
that needs it.
@terryjreedy terryjreedy added docs Documentation in the Doc dir skip news needs backport to 3.11 only security fixes needs backport to 3.12 bug and security fixes labels Jul 15, 2023
@terryjreedy terryjreedy self-assigned this Jul 15, 2023
@methane
Copy link
Member

methane commented Jul 16, 2023

How about using dataclass or namedtuple with comment?

@terryjreedy
Copy link
Member Author

@methane Class Point without the added line appears at line 309 followed by a match statement whose cases only use keyword matching. The paragraph that follow mentions using match_args to allow cases using positional matching. This augmented class and the match that follows illustrates this point. Without this, using the previous definition of Point, this example fails. So I consider this the proper fix for this issue. Note that the primary purpose of this example is to show nested patterns, in this case class(es) inside of a sequence. Showing positional matching is secondary.

This is the tutorial and to me, namedtuples and dataclasses are a more advanced topic. (I have never used the latter.) I think they should be another issue.

@terryjreedy terryjreedy merged commit 7aa89e5 into python:main Jul 17, 2023
15 checks passed
@miss-islington
Copy link
Contributor

Thanks @terryjreedy for the PR 🌮🎉.. I'm working now to backport this PR to: 3.11, 3.12.
🐍🍒⛏🤖

@bedevere-bot
Copy link

GH-106819 is a backport of this pull request to the 3.12 branch.

@bedevere-bot bedevere-bot removed the needs backport to 3.12 bug and security fixes label Jul 17, 2023
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request 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 <tjreedy@udel.edu>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request 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 <tjreedy@udel.edu>
@bedevere-bot
Copy link

GH-106820 is a backport of this pull request to the 3.11 branch.

@bedevere-bot bedevere-bot removed the needs backport to 3.11 only security fixes label Jul 17, 2023
@terryjreedy terryjreedy deleted the posarg branch July 17, 2023 03:37
terryjreedy added a commit that referenced this pull request 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 <tjreedy@udel.edu>
terryjreedy added a commit that referenced this pull request 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 <tjreedy@udel.edu>
@methane
Copy link
Member

methane commented Jul 17, 2023

The paragraph that follow mentions using match_args to allow cases using positional matching. This augmented class and the match that follows illustrates this point.

Both of dataclasses and __match_args__ are mentioned:

 You can use positional parameters with some builtin classes that provide an
 ordering for their attributes (e.g. dataclasses). You can also define a specific
 position for attributes in patterns by setting the ``__match_args__`` special
 attribute in your classes.

This is the tutorial and to me, namedtuples and dataclasses are a more advanced topic. (I have never used the latter.) I think they should be another issue.

I feel manually define __match_args__ is more advanced than just using dataclass.
I used dataclasses, namedtuples, and similar objects many times. But I never write __matcch_args__ manually except testing match statement behavior.

Anyway, it is a matter of preference and either is acceptable.

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 skip news
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants