Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

bpo-42128: Add what's new section for PEP 634 (pattern matching)#24588

Closed
gvanrossum wants to merge 1 commit into
python:masterpython/cpython:masterfrom
gvanrossum:whatsnew-pattern-matchinggvanrossum/cpython:whatsnew-pattern-matchingCopy head branch name to clipboard
Closed

bpo-42128: Add what's new section for PEP 634 (pattern matching)#24588
gvanrossum wants to merge 1 commit into
python:masterpython/cpython:masterfrom
gvanrossum:whatsnew-pattern-matchinggvanrossum/cpython:whatsnew-pattern-matchingCopy head branch name to clipboard

Conversation

@gvanrossum

@gvanrossum gvanrossum commented Feb 19, 2021

Copy link
Copy Markdown
Member

Obviously this shouldn't be merged before the main pattern matching PR (GH-22917).

https://bugs.python.org/issue42128

Comment thread Doc/whatsnew/3.10.rst
Comment on lines +139 to +158
The simplest form compares a subject value against one or more literals::

def http_error(status):
match status:
case 400:
return "Bad request"
case 404:
return "Not found"
case 418:
return "I'm a teapot"
case _:
return "Something's wrong with the Internet"

Note the last block: the "variable name" ``_`` acts as a *wildcard* and
never fails to match.

You can combine several literals in a single pattern using ``|`` ("or")::

case 401 | 403 | 404:
return "Not allowed"

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example has some problems, as mentioned in https://bugs.python.org/issue42128#msg387371:

I've seen too many knee-jerk reactions over the past weeks along the lines of "the new switch feature can't handle named constants!".

Interestingly I haven't seen such reactions, I've just seen a lot of (mostly) positive responses to my tweet linking to this same tutorial: https://twitter.com/gvanrossum/status/1361124478671933443. (Admittedly, there were negative reactions too, including "the worst pep of the python history." But I didn't see anything about named constants.)

@willingc willingc self-requested a review February 20, 2021 04:47
@willingc willingc self-assigned this Feb 20, 2021
@Fidget-Spinner

Fidget-Spinner commented Feb 21, 2021

Copy link
Copy Markdown
Member

Guido, thanks for updating what's new. What do you think of adding a small FAQ section in the what's new too? Or just mentioning that the new statements use soft keywords and old identifiers won't break?

I've seen quite a bit of FUD online, and I admit initially thinking my own code using match and case would break before I read the PEP thoroughly. So I think that placing a sentence in bold would help allay unfounded fears.

Maybe:

The match statement uses soft keywords.  Only lines beginning with ``match``, containing a subject, a colon, and a line break will be identified as a match statement.  Existing code using ``match`` as identifiers for variable names or functions will not break.

Alternatively, copied straight from the PEP:

The match and case keywords are *soft keywords*, i.e. they
are not reserved words in other grammatical contexts (including at the start of
a line if there is no colon where expected). This implies that they are
recognized as keywords when part of a match statement or case block only, and
are allowed to be used in all other contexts as variable or argument names.

BTW, Daniel and I are tracking some of our own docs works here https://github.com/dmoisset/cpython/issues , though admittedly I haven't done much yet and I plan to sprint it over this weekend ;).

@gvanrossum

Copy link
Copy Markdown
Member Author

What do you think of adding a small FAQ section in the what's new too? Or just mentioning that the new statements use soft keywords and old identifiers won't break?

Thanks for the reminder -- this indeed an important point. I believe this PR is likely dead, and it looks like @willingc is going to write us a better section.

@willingc

Copy link
Copy Markdown
Contributor

@gvanrossum I'm half way through the What's New and should post it tomorrow.

@gvanrossum

Copy link
Copy Markdown
Member Author

Thanks @willingc just in time for 3.10a6!

@willingc

willingc commented Mar 1, 2021

Copy link
Copy Markdown
Contributor

@gvanrossum Any objections to closing this PR?

@gvanrossum

Copy link
Copy Markdown
Member Author

Sorry, forgot!

@gvanrossum gvanrossum closed this Mar 1, 2021
@gvanrossum gvanrossum deleted the whatsnew-pattern-matching branch August 7, 2022 15:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting core review docs Documentation in the Doc dir skip news

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

Morty Proxy This is a proxified and sanitized view of the page, visit original site.