# Addition of addition to match case

**URL:** <https://discuss.python.org/t/addition-of-addition-to-match-case/16185>\
**Category:** Python Help\
**Created:** [June 1, 2022, 10:04pm UTC](https://discuss.python.org/t/addition-of-addition-to-match-case/16185 "2022-06-01T22:04:29Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![vainaixr](https://avatars.discourse-cdn.com/v4/letter/v/5f8ce5/32.png) [@vainaixr](https://discuss.python.org/u/vainaixr)\
**Post date:** [June 1, 2022, 10:04pm UTC](https://discuss.python.org/t/addition-of-addition-to-match-case/16185/1 "2022-06-01T22:04:29Z")

</div>

current scenario -

1. 

```auto
x = 'cd'
match x:
  case ('c' + 'd') | 'e':
    print(1)
  case 'a':
    print(2)

```

gives,

```auto
    case ('c' + 'd') | 'e':
              ^
SyntaxError: invalid syntax

```

expected scenario -

1. addition/concatenation works in match case, just like it does outside of match case and the above outputs `1`

---

<div class="post-metadata">

**Author:** ![steven.daprano](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steven.daprano/32/1083_2.png) [@steven.daprano](https://discuss.python.org/u/steven.daprano)\
**Post date:** [June 1, 2022, 10:16pm UTC](https://discuss.python.org/t/addition-of-addition-to-match-case/16185/2 "2022-06-01T22:16:04Z")

</div>

What is so special about _addition_ only that you want to support addition, and not every other unlimited expression?

Have you read the PEPs on pattern matching to see why expressions aren’t supported?

---

<div class="post-metadata">

**Author:** ![vainaixr](https://avatars.discourse-cdn.com/v4/letter/v/5f8ce5/32.png) [@vainaixr](https://discuss.python.org/u/vainaixr)\
**Post date:** [June 1, 2022, 10:30pm UTC](https://discuss.python.org/t/addition-of-addition-to-match-case/16185/3 "2022-06-01T22:30:28Z")

</div>

yes it turns out `*`, `-` none of it work.

yes I have read the PEPs, but mostly for code that works.

where have they mentioned the reasoning for it, I could not find it, as there are four PEPs for match case and it is a bit difficult to find in them.

---

<div class="post-metadata">

**Author:** ![jeanas](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jeanas/32/8115_2.png) [@jeanas](https://discuss.python.org/u/jeanas)\
**Post date:** [June 1, 2022, 10:45pm UTC](https://discuss.python.org/t/addition-of-addition-to-match-case/16185/4 "2022-06-01T22:45:32Z")

</div>

What would be the semantics?

```auto
match foo:
    case bar: # matches everything
        ...

match foo:
    case (bar, baz): # matches 2-tuples
        ...

match foo:
    case bar + baz: # what does this match?
         ...

```

---

<div class="post-metadata">

**Author:** ![vainaixr](https://avatars.discourse-cdn.com/v4/letter/v/5f8ce5/32.png) [@vainaixr](https://discuss.python.org/u/vainaixr)\
**Post date:** [June 1, 2022, 10:57pm UTC](https://discuss.python.org/t/addition-of-addition-to-match-case/16185/5 "2022-06-01T22:57:16Z")

</div>

probably ambiguous types should not be valid, but,  
for concatenation,

```auto
match x:
  case str('x') + str(): # matches any string that begins with 'x'

```

```auto
match x:
  case list('a') + list(): # matches any list, that has first element as 'a'

```

could be a possibility

---

<div class="post-metadata">

**Author:** ![jeanas](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jeanas/32/8115_2.png) [@jeanas](https://discuss.python.org/u/jeanas)\
**Post date:** [June 2, 2022, 6:45am UTC](https://discuss.python.org/t/addition-of-addition-to-match-case/16185/6 "2022-06-02T06:45:35Z")

</div>

How do you tell if a type is ambiguous or not?

For what it’s worth, the first one can be spelt as `x.startswith('x')`, and I don’t have use cases in mind where the sequence of characters in a string lends itself well to pattern matching rather than such string processing methods. The second one can already be spelt as

```auto
match x:
    case ('a', *other):
        ...

```

(I was imprecise in my other reply: `(foo, bar)` matches sequences, not just tuples.)

---

<div class="post-metadata">

**Author:** ![vainaixr](https://avatars.discourse-cdn.com/v4/letter/v/5f8ce5/32.png) [@vainaixr](https://discuss.python.org/u/vainaixr)\
**Post date:** [June 2, 2022, 8:39am UTC](https://discuss.python.org/t/addition-of-addition-to-match-case/16185/7 "2022-06-02T08:39:30Z")

</div>

on further investigation, I found out that there is some issue with tuple vs list matching using the technique you described.

```auto
x = ['a', 'b', 'c', 'd']
y = ('a', 'b', 'c')
match y:
  case ['a', *others]:
    print(1)
  case ('a', *others):
    print(2)

```

gives `1`, which should give `2`  
one way to make it work is this,

```auto
x = ['a', 'b', 'c', 'd']
y = ('a', 'b', 'c')
match y:
  case list(['a', *others]):
    print(1)
  case tuple(('a', *others)):
    print(2)

```

which gives `2`

I am still investigating, use cases for these operators to work in match case, as it is not only `+`, but even other operators, like `*`, `-`.  
but the main reason is to make the operators work the same way as they do outside of the match case statement for these types.

for example, if one wants to do a exact list match for,

```auto
x = [1, 2] * 10000
match x:
  case list([1, 2, 1, 2, ...]): # in the current scenario, would have to write it 10000 times, which is not feasible

```

better would have been,

```auto
x = [1, 2] * 10000
match x:
  case list([1, 2] * 10000):

```

but this syntax is invalid currently.

---

<div class="post-metadata">

**Author:** ![saaketp](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/saaketp/32/6703_2.png) [@saaketp](https://discuss.python.org/u/saaketp)\
**Post date:** [June 2, 2022, 8:58am UTC](https://discuss.python.org/t/addition-of-addition-to-match-case/16185/8 "2022-06-02T08:58:20Z")

</div>

Square bracket and parenthesis both match sequences, just like it is mentioned in PEP 636

> **[PEP 636 – Structural Pattern Matching: Tutorial | peps.python.org](https://peps.python.org/pep-0636/#matching-sequences)**
>
> Python Enhancement Proposals (PEPs)

You’re probably misunderstanding what case does, it is pattern match, not a dynamic comparison of values, you should use simple if else statements for that, or you can use `case ... if ...`:

```auto
x = [1, 2] * 10000
match x:
  case list() if x == [1, 2] * 10000:
    print("matches")

```

---

<div class="post-metadata">

**Author:** ![vainaixr](https://avatars.discourse-cdn.com/v4/letter/v/5f8ce5/32.png) [@vainaixr](https://discuss.python.org/u/vainaixr)\
**Post date:** [June 2, 2022, 11:34am UTC](https://discuss.python.org/t/addition-of-addition-to-match-case/16185/9 "2022-06-02T11:34:31Z")

</div>

upon further investigation I found one more way to achieve this, that is with the use of class attributes,

```auto
class A:
  lst = [1, 2] * 100
x = [1, 2] * 100

match x:
  case A.lst:
    print(1)

```

> [@saaketp](#):
>
> ` case list() if x == [1, 2] * 10000:`

I personally prefer the class attribute way over using `if` in the match case.

---

<div class="post-metadata">

**Author:** ![steven.daprano](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steven.daprano/32/1083_2.png) [@steven.daprano](https://discuss.python.org/u/steven.daprano)\
**Post date:** [June 2, 2022, 11:33am UTC](https://discuss.python.org/t/addition-of-addition-to-match-case/16185/10 "2022-06-02T11:33:20Z")

</div>

Using `case str('x') + str()` for string prefixes is an interesting  
idea, but that can already be done using guard clauses:

```auto
match value:
    case str() as s if s.startswith(prefix):
        ...

```
