Questions tagged [structural-pattern-matching]

This tag is for questions about Python's Structural Pattern Matching feature, introduced in version 3.10

PEP 634 introduced the specification of the Structural Pattern Matching feature starting with . In addition, PEP 635 introduces the motivations and rationale behind this new feature. Lastly, PEP 636 introduces a hands-on tutorial of the different ways to use this feature.

The feature offers a convenient, dynamic way to parse different inputs against desired patterns, and to specify the ways to handle these patterns accordingly. This is done using the new soft keywords ( i.e. they are not reserved words in other grammatical contexts) match and case.

For example:

match string:
    case "value1":
        # do something if the input is "value1"
    case _:
        # do something for any other case

will match the input string against the different cases (patterns), when _ is used as a wildcard - the default case which always matches.

Some of the other useful constructs are:

52 questions
0
votes
0 answers

How to use 'in' a python match case statement

I'd like to check if a value is in a list in the 'case' of a structural pattern matching. Put it another way, I'd like to translate the following "if else" in a match case statement. list1 = [4, 2, 7] var = 2 if var == 1: print("case1") elif…
u2gilles
  • 6,888
  • 7
  • 51
  • 75
0
votes
2 answers

Match/Case used to decide what keys JSON response contains

I am trying to apply a match/case in my script where I want to do some action based on the JSON response from the API call: response_types = 1. -> {'data': [{'id': 1485037059173588994}]} 2. -> {'data': [{'media': 1423364523411623943}]} 3. ->…
0
votes
1 answer

How to call a function inside python's match pattern?

I have code join_prefix = lambda text: config.get("CM_PREFIX") + text match shlex.split(message.message): case [join_prefix("setmode"), mode] if mode.isdigit(): await self._set_mode_handler(message, int(mode)) return case…
h3xcode
  • 42
  • 8
0
votes
1 answer

Python Structural pattern matching - pass Object to case statement

this is my code class BasePlayer: @staticmethod def walk(direction): print(f"I run into {direction}") class Archer(BasePlayer): @staticmethod def shoot(): print("shoot") class Wizard(BasePlayer): @staticmethod …
Data Mastery
  • 1,555
  • 4
  • 18
  • 60
0
votes
1 answer

Confusion between use of `|` to combine two dictionaries vs in match-case

we know that | is used to combine two dictionaries, like, dct_1 = {'a': 1} dct_2 = {'b': 2} print(dct_1 | dct_2) gives, {'a': 1, 'b': 2} but if one wants to use the same | in match-case to combine two dictionaries, x = {'a': 1, 'b': 2} d = {'a':…
apostofes
  • 2,959
  • 5
  • 16
  • 31
0
votes
1 answer

Matching an element in an unknown sequence location using structural pattern matching in Python 3.10

Is there any clever way to match on an element in an unknown location in a sequence of unknown length using structural pattern matching in Python 3.10? Below is a non-working example illustrating what I'd like to do. match [1, 2, "3", 4, 5]: …
Daniel Hjertholm
  • 177
  • 1
  • 2
  • 16
-1
votes
1 answer

How to implement @dataclass to define arithmetic operations in Python?

I'm learning Python on my own and I found a task that requires using a decorator @dataclass to create a class with basic arithmetic operations. from dataclasses import dataclass from numbers import Number @dataclass class MyClass: x: float …
1 2 3
4