# Gauging sentiment on pattern matching

**URL:** <https://discuss.python.org/t/gauging-sentiment-on-pattern-matching/5770>\
**Category:** Committers\
**Created:** [November 16, 2020, 7:18pm UTC](https://discuss.python.org/t/gauging-sentiment-on-pattern-matching/5770 "2020-11-16T19:18:10Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![brettcannon](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/brettcannon/32/34895_2.png) [@brettcannon](https://discuss.python.org/u/brettcannon)\
**Post date:** [November 16, 2020, 7:18pm UTC](https://discuss.python.org/t/gauging-sentiment-on-pattern-matching/5770/1 "2020-11-16T19:18:10Z")

</div>

The SC is trying to see whether there is clear consensus on pattern matching in general and [PEP 634](https://www.python.org/dev/peps/pep-0634/) specifically. Please vote below to express your current feeling on this topic.

To be clear, this is not a binding vote, but instead to see if any consensus exists among the core team. This poll will close on Monday, Nov 23.

_Poll ([view on site](https://discuss.python.org/t/gauging-sentiment-on-pattern-matching/5770/1))_

---

<div class="post-metadata">

**Author:** ![pganssle](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/pganssle/32/245_2.png) [@pganssle](https://discuss.python.org/u/pganssle)\
**Post date:** [November 16, 2020, 7:27pm UTC](https://discuss.python.org/t/gauging-sentiment-on-pattern-matching/5770/2 "2020-11-16T19:27:28Z")

</div>

I kept up with the original pattern matching PEP for a while, but the discussion has splintered so much since then that I’ve had a really hard time keeping up with the current state of affairs.

Maybe it would help give better results or more voter turnout if someone who has been could prepare a very short TL;DR for the salient features of each of these things? (Or maybe not — maybe it would just introduce a bunch of noise from people going with their gut feelings rather than looking at the careful reasoning in the PEP and the back-and-forth discussions).

Sorry for the non-answer here.

---

<div class="post-metadata">

**Author:** ![gpshead](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/gpshead/32/54_2.png) [@gpshead](https://discuss.python.org/u/gpshead)\
**Post date:** [November 16, 2020, 7:52pm UTC](https://discuss.python.org/t/gauging-sentiment-on-pattern-matching/5770/3 "2020-11-16T19:52:20Z")

</div>

I want pattern matching but haven’t been able to spend time reading recent PEPs (let alone infinite emails).

---

<div class="post-metadata">

**Author:** ![brettcannon](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/brettcannon/32/34895_2.png) [@brettcannon](https://discuss.python.org/u/brettcannon)\
**Post date:** [November 16, 2020, 7:57pm UTC](https://discuss.python.org/t/gauging-sentiment-on-pattern-matching/5770/4 "2020-11-16T19:57:14Z")

</div>

> [@pganssle](#):
>
> Maybe it would help give better results or more voter turnout if someone who has been could prepare a very short TL;DR for the salient features of each of these things?

You sort of have that:

- [PEP 634](https://www.python.org/dev/peps/pep-0634/) is the spec
- [PEP 635](https://www.python.org/dev/peps/pep-0635/) is the rationale
- [PEP 636](https://www.python.org/dev/peps/pep-0636/) is a tutorial

So PEP 636 might cover what you’re after.

---

<div class="post-metadata">

**Author:** ![pganssle](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/pganssle/32/245_2.png) [@pganssle](https://discuss.python.org/u/pganssle)\
**Post date:** [November 16, 2020, 8:01pm UTC](https://discuss.python.org/t/gauging-sentiment-on-pattern-matching/5770/5 "2020-11-16T20:01:47Z")

</div>

> [@brettcannon](#):
>
> You sort of have that:
> 
> - [PEP 634](https://www.python.org/dev/peps/pep-0634/) is the spec
> - [PEP 635](https://www.python.org/dev/peps/pep-0635/) is the rationale
> - [PEP 636](https://www.python.org/dev/peps/pep-0636/) is a tutorial
> 
> So PEP 636 might cover what you’re after.

I was more hoping for something like “here is the list of stuff that people found contentious, and here’s how PEP 634, PEP 640 and PEP 642 handle it differently (if at all).”

---

<div class="post-metadata">

**Author:** ![brettcannon](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/brettcannon/32/34895_2.png) [@brettcannon](https://discuss.python.org/u/brettcannon)\
**Post date:** [November 16, 2020, 8:13pm UTC](https://discuss.python.org/t/gauging-sentiment-on-pattern-matching/5770/6 "2020-11-16T20:13:06Z")

</div>

I think the variety and volume is too big to probably get a good summary of it. Honestly, I bet the [Rationale section of PEP 635](https://www.python.org/dev/peps/pep-0635/#rationale) covers the topics of what typically came up.

---

<div class="post-metadata">

**Author:** ![brandtbucher](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/brandtbucher/32/22862_2.png) [@brandtbucher](https://discuss.python.org/u/brandtbucher)\
**Post date:** [November 16, 2020, 8:26pm UTC](https://discuss.python.org/t/gauging-sentiment-on-pattern-matching/5770/7 "2020-11-16T20:26:50Z")

</div>

> I was more hoping for something like “here is the list of stuff that people found contentious, and here’s how PEP 634, PEP 640 and PEP 642 handle it differently (if at all).”

In short:

- PEP 634 uses `_` and `*_` for non-binding wildcards. PEP 640 proposes using `?` and `*?` instead, and also allows them to be used in most assignments throughout the language.

- PEP 642 differs in many ways, and it appears these rules are still in flux to some degree. Its [Abstract](https://www.python.org/dev/peps/pep-0642/#abstract) has a pretty good listing of differences with PEP 634.

---

<div class="post-metadata">

**Author:** ![guido](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/guido/32/21_2.png) [@guido](https://discuss.python.org/u/guido)\
**Post date:** [November 16, 2020, 8:35pm UTC](https://discuss.python.org/t/gauging-sentiment-on-pattern-matching/5770/8 "2020-11-16T20:35:58Z")

</div>

> [@pganssle](#):
>
> I was more hoping for something like “here is the list of stuff that people found contentious, and here’s how PEP 634, PEP 640 and PEP 642 handle it differently (if at all).”

Perhaps the most important concern that’s not represented in an additional PEP yet is whether capture variables should be marked with some sigil. If you want this you should probably choose “I want pattern matching, but not as defined in those PEPs”. (But don’t vote that way until you’ve read the [section in PEP 635](https://www.python.org/dev/peps/pep-0635/#capture-patterns) about this.)

---

<div class="post-metadata">

**Author:** ![pitrou](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/pitrou/32/28_2.png) [@pitrou](https://discuss.python.org/u/pitrou)\
**Post date:** [November 16, 2020, 9:11pm UTC](https://discuss.python.org/t/gauging-sentiment-on-pattern-matching/5770/9 "2020-11-16T21:11:57Z")

</div>

1. On a personal basis, I feel no strong need for pattern matching and I don’t really care about it. My life won’t be made significantly easier with it.
2. From a language design POV, I feel pattern matching is one more step that makes Python less approachable for casual users, while it will bring little gain even for expert users.
3. I would strongly support a long-term moratorium on language additions.

---

<div class="post-metadata">

**Author:** ![yselivanov](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/yselivanov/32/491_2.png) [@yselivanov](https://discuss.python.org/u/yselivanov)\
**Post date:** [November 16, 2020, 10:16pm UTC](https://discuss.python.org/t/gauging-sentiment-on-pattern-matching/5770/10 "2020-11-16T22:16:28Z")

</div>

I want pattern matching and in my opinion PEP 634 is good as is.

Few thoughts:

- Before voting I’d recommend people to look at [https://www.python.org/dev/peps/pep-0636/](https://www.python.org/dev/peps/pep-0636/).

- Pattern matching is something that I briefly used in erlang and thoroughly enjoyed. I think it has a great potential to enhance async/await code (better syntax for implementing events/messaging). I’m excited to see what people will build with it.

- Pattern matching is extremely useful for compilers and parsers. Something that I have a fair share of experience with, and I’d love Python to be more expressive at that kind of code.

- I generally find arguments like “Python would become harder to learn” or “Python would become less approachable” to be relatively weak. Beginners don’t use all language features on day 1, and pattern matching as presented is quite intuitive. On the other hand, the new syntax would simplify the spaghetti code that professionals have to work with on a daily basis.

- Python used to be Guido’s language. And it somehow evolved under his vision to be in the top 5 languages today. PEP 634 is something Guido has been working for a couple of years, at least. He considered multiple cons and pros of the proposal and still thinks this is a great addition, so even if I didn’t want pattern matching, I’d still trust him here.

As for PEP 640: I think that having a special treatment for `_` in PEP 634 is acceptable. I’d personally be OK with using `?` instead of `_` in `match..case` only, but accepting the entire PEP 640 would mean that we’d see things like `for ? in range(3)` – something that doesn’t look like Python to me.

---

<div class="post-metadata">

**Author:** ![malemburg](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/malemburg/32/50_2.png) [@malemburg](https://discuss.python.org/u/malemburg)\
**Post date:** [November 17, 2020, 11:23am UTC](https://discuss.python.org/t/gauging-sentiment-on-pattern-matching/5770/11 "2020-11-17T11:23:36Z")

</div>

I have read through the PEPs 634, 635 and 636 and have a couple of questions:

- How would the proposal deal with literals which are defined via a function calls or constants ?

- PEP 636:

- Experimenting with the syntax:

I’ve also had a look at PEP 640 and 642, but don’t really like the syntax these propose. We have to be careful not to create too much line noise in Python.

For now, my vote would go to option 4, but I’ll wait until perhaps someone can educate me on the above questions. TIA 🙂

---

<div class="post-metadata">

**Author:** ![Senthil](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/senthil/32/18300_2.png) [@Senthil](https://discuss.python.org/u/Senthil)\
**Post date:** [November 17, 2020, 12:07pm UTC](https://discuss.python.org/t/gauging-sentiment-on-pattern-matching/5770/12 "2020-11-17T12:07:44Z")

</div>

I voted for PEP 634 as-is.

My other consideration would have been PEP 634 + PEP 640. But, I saw PEP 640 as a standalone proposal in itself.

The use of `_` for captured-but-unassigned in PEP 634, used with similar to how I have used “\_” in python programs.

I was also a little concerned when I read if “or patterns”, “conditional patterns”, “sub-patterns” support could increase the reading complexity of the python programs, but I am not certain. They could, but it depends on the programmer writing the code, and using these (sub) tools of pattern matching a programmer could write meaningful code too.

---

<div class="post-metadata">

**Author:** ![pf\_moore](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/pf_moore/32/35_2.png) [@pf\_moore](https://discuss.python.org/u/pf_moore)\
**Post date:** [November 17, 2020, 12:30pm UTC](https://discuss.python.org/t/gauging-sentiment-on-pattern-matching/5770/13 "2020-11-17T12:30:06Z")

</div>

I voted PEP 634 as-is.

I think pattern matching would be a useful feature. I can easily imagine using it.

Reading PEP 636 (the tutorial) gave me a _very_ good sense of the logic and expected uses of the new feature, and everything felt completely natural in that context. Yes, it’s a tutorial designed to show off the proposed feature, so you’d expect that, but it reassured me that the choices made look sensible and work well in a realistic context (at least in one such).

I don’t like ? as a placeholder, so 640 has no appeal to me, _especially_ because it contradicts the common idiom of using `_` in existing Python code.

I wish I understood the point behind 642, but it seems to be a variation without a unified theme to it, and the only impression I really get is that I’d need to put `==` all over the place when matching. (I know that seems a little unfair, and I’m sorry, but in terms of my “sentiment”, it’s general impressions that count).

---

<div class="post-metadata">

**Author:** ![brandtbucher](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/brandtbucher/32/22862_2.png) [@brandtbucher](https://discuss.python.org/u/brandtbucher)\
**Post date:** [November 17, 2020, 5:03pm UTC](https://discuss.python.org/t/gauging-sentiment-on-pattern-matching/5770/14 "2020-11-17T17:03:17Z")

</div>

Thanks for your questions, @malemburg.

> How would the proposal deal with literals which are defined via a function calls or constants ?

Simply put: the results of those function calls and the values of those constants would need to be put in a qualified namespace.

```py
>>> class YN:
... YES = gettext("yes")
... NO = gettext("no")
... 
>>> match "ja":
... case YN.YES: ...
... case YN.NO: ...
...

```

(You can also use guards for more complicated cases).

Anything “called” basically works as an `isinstance` check, with the possibility for further destructuring. You’ll get a nice error message if you try to match on something other than a type at run-time:

```py
>>> match "ja":
... case gettext("yes"): ...
... case gettext("no"): ...
... 
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
TypeError: called match pattern must be a type

```

Similarly, you can have at most one irrefutable pattern. So in practice, trying to use pattern matching as a switch statement on unqualified constants will guide you in the right direction as well (with a `SyntaxError` at compile-time):

```py
>>> match "ja":
... case YES: ...
... case NO: ...
...
  File "<stdin>", line 2
SyntaxError: name capture 'YES' makes remaining patterns unreachable

```

> In PEP 636, the “Appendix A” gives a good example: you would not put literal 404 into a case block, but rather use HTTP\_NOT\_FOUND.

Or, even better: `HTTPStatus.NOT_FOUND` from the `http` module.

> Since this kind of literal matching is likely going to be the most common use case of the syntax, I think the PEPs should give more attention to this case, not just mention it in an appendix.

The rejections of PEPs [275](https://www.python.org/dev/peps/pep-0275/) and [3103](https://www.python.org/dev/peps/pep-3103/) were our first hint that this would _not_ be the most common use case of the new syntax. It’s clear that the community needs a different feature - one that can’t be replaced by a dictionary or a trivial `if`-`elif`-`else` ladder. We found, based on a survey of other languages and past discussions, that a destructuring approach (where you match the shape of the object and extract interesting parts from it) was the most promising. We provide the ability to match on equality with a literal or constant as a convenience, not as a core feature.

In fact, the first draft of [PEP 622](https://www.python.org/dev/peps/pep-0622/) (superseded by PEP 634) allowed you to use `.YES` (note the leading dot) to indicate an equality comparison, rather than a capture. It also had the nice effect of having an easy-to-remember rule that “any dotted name in a pattern is a load, not a store”. Unfortunately, this got probably the most pushback of anything in the proposal (too different from the rest of the language, too hard to see, doesn’t make automatic refactoring better, etc.). So we dropped it. If we wanted to add something similar in the future, though, the door is always open to do so. So we can see if this really does cause problems in practice, and respond if necessary.

> Under “Matching builtin classes”, why do you have to write str() in `{"text": str() as message, "color": str() as c}` and not just str (without the parens, giving the type object) ?

Because otherwise it would be ambiguous with capture patterns. The call syntax also allows you to destructure the match subject further by attributes (which, again, is where pattern matching really shines).

It’s sort of the same reason why we can’t just omit the parentheses of zero-argument function/method calls. `f` would be ambiguous, while `f()` is not.

Also, note that this is more simply spelled as `{"text": str(message), "color": str(c)}`.

> The str() would evaluate to an empty string (at least intuitively), just like str(“”), but the matching really is about the type and not the string value.

Yes, but it’s about more than _just_ the type. While `str()` is a bit boring, you can zoom out and look to the mapping pattern used in this same example to see why the internal structure can be just as important (if not _more_ important) than the actual type.

> Would it be possible to add the syntax only with a `from __future__ import pattern_matching_v1` marker to experiment with it for a release or two before making it final ?
> 
> This would give the syntax a chance to mature in practice and at the same make it obvious to readers of the code that the syntax may in fact still change in an upcoming release.

The way I see it, that’s what development, alphas, and betas are for. You can even [play with it now](https://mybinder.org/v2/gh/gvanrossum/patma/master?urlpath=lab/tree/playground-622.ipynb), if you want to!

---

<div class="post-metadata">

**Author:** ![stoneleaf](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/stoneleaf/32/88_2.png) [@stoneleaf](https://discuss.python.org/u/stoneleaf)\
**Post date:** [November 17, 2020, 10:12pm UTC](https://discuss.python.org/t/gauging-sentiment-on-pattern-matching/5770/15 "2020-11-17T22:12:34Z")

</div>

> [@malemburg](#):
>
> Would it be possible to add the syntax only with a `from __future__ import pattern_matching_v1` marker to experiment with it for a release or two before making it final ?

> [@brandtbucher](#):
>
> The way I see it, that’s what development, alphas, and betas are for.

However, that gives us – at most and under ideal circumstances – 12 months. IIRC `asyncio` was a ` __future__ ` import for, what, two releases? And those were 18-month releases.

Having said that, I’m fine with going with PEPs 634-6 as I could and would use them right now.

---

<div class="post-metadata">

**Author:** ![brandtbucher](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/brandtbucher/32/22862_2.png) [@brandtbucher](https://discuss.python.org/u/brandtbucher)\
**Post date:** [November 17, 2020, 10:30pm UTC](https://discuss.python.org/t/gauging-sentiment-on-pattern-matching/5770/16 "2020-11-17T22:30:56Z")

</div>

> [@stoneleaf](#):
>
> IIRC `asyncio` was a ` __future__ ` import for, what, two releases?

For my own understanding, which PEP / ` __future__ ` import is this? `async` and `await` became hard keywords in 3.7, but I think that was due to compatibility concerns (not a provisional period or anything).

---

<div class="post-metadata">

**Author:** ![asvetlov](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/asvetlov/32/55_2.png) [@asvetlov](https://discuss.python.org/u/asvetlov)\
**Post date:** [November 17, 2020, 10:32pm UTC](https://discuss.python.org/t/gauging-sentiment-on-pattern-matching/5770/17 "2020-11-17T22:32:30Z")

</div>

Speaking about asyncio, the library did never require `from __future__ import ...`.  
Instead, it had a _provisional_ status which means a freedom to add backward incompatible changes if there is no choice.  
IIRC typing was provisional as well.

---

<div class="post-metadata">

**Author:** ![stoneleaf](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/stoneleaf/32/88_2.png) [@stoneleaf](https://discuss.python.org/u/stoneleaf)\
**Post date:** [November 17, 2020, 11:12pm UTC](https://discuss.python.org/t/gauging-sentiment-on-pattern-matching/5770/18 "2020-11-17T23:12:54Z")

</div>

> [@asvetlov](#):
>
> Instead, it had a _provisional_ status which means a freedom to add backward incompatible changes if there is no choice.

Ah, right – thanks for the clarification!

---

<div class="post-metadata">

**Author:** ![steve.dower](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steve.dower/32/56_2.png) [@steve.dower](https://discuss.python.org/u/steve.dower)\
**Post date:** [November 18, 2020, 9:40am UTC](https://discuss.python.org/t/gauging-sentiment-on-pattern-matching/5770/19 "2020-11-18T09:40:42Z")

</div>

And the result of those two provisional packages was that “provisional” was deemed more trouble than it’s worth, and so we don’t do them anymore (unless you convince the SC to forget that previous decision and do it anyway).

Better to find ways to validate the design before release and commit to it. Python has too many users to get away with blatant API changes these days.

---

<div class="post-metadata">

**Author:** ![thomas](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/thomas/32/5492_2.png) [@thomas](https://discuss.python.org/u/thomas)\
**Post date:** [November 18, 2020, 9:42am UTC](https://discuss.python.org/t/gauging-sentiment-on-pattern-matching/5770/20 "2020-11-18T09:42:06Z")

</div>

Besides, ‘provisional’ applies to _packages_ (see PEP 411), not to language features.

[Next page](https://discuss.python.org/t/gauging-sentiment-on-pattern-matching/5770.md?page=2)
