Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

234
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda