Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

199
Visualizações
Defining a match-case statement with the same instruction for two different cases in Python

I would like to create a match-case statement in Python in which two cases do the same instruction. For instance, if Python behaved like C++, according to this Stack Overflow question, I would write the match-case statement as:

        match x:
            case 1:
            case 2:
                # Some code
                break
            case 3:
                # Some code
                break

The code however does not work, showing that the match-case statement needs to be written in a different way in Python. What is this way?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

match x:
    case 1 | 2:
        # Some code
    case 3:
        # Some code

Python match clauses don't fall through, so you can't concatenate them as in C and you don't use break at the end of the clause.

The "OR pattern" (as shown, where | means "or") can be used with subpatterns which bind variables, but all the subpatterns need to bind the same variables.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda