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

480
Visualizações
How does Python 3.10 match compares 1 and True?

PEP 622, Literal Patterns says the following:

Note that because equality (__eq__) is used, and the equivalency between Booleans and the integers 0 and 1, there is no practical difference between the following two:

case True:
    ...

case 1:
    ...

and True.__eq__(1) and (1).__eq__(True) both returns True, but when I run these two code snippets with CPython, it seems like case True and case 1 are not same.

$ python3.10
>>> match 1:
...     case True:
...         print('a')  # not executed
... 
>>> match True:
...     case 1:
...         print('a')  # executed
... 
a

How are 1 and True actually compared?

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

0

Looking at the pattern matching specification, this falls under a "literal pattern":

A literal pattern succeeds if the subject value compares equal to the value expressed by the literal, using the following comparisons rules:

  • Numbers and strings are compared using the == operator.
  • The singleton literals None, True and False are compared using the is operator.

So when the pattern is:

 case True:

It uses is, and 1 is True is false. On the other hand,

case 1:

Uses ==, and 1 == True is true.

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