0
a = True

match a:
    case False:
        print("It is false")
    case 2:
        print('It is 2 true.')
    case 1:
        print('It is 1 true.')

>>> It is 1 true.

Why does the program execute the last case statement?

sp1rs
  • 786
  • 8
  • 23
  • What does `print(True == 1)` show? – Barmar Feb 06 '23 at 20:39
  • 1
    of course it does. `True == 1` and `isinstance(True, int)` is true. Admittedly, this is a bit of a wart in the Python type system, but "practicality beats purity". – juanpa.arrivillaga Feb 06 '23 at 20:40
  • to clarify: `bool` is a subclass of `int`. So `True==1` and `False==0` (AFAIK). – rv.kvetch Feb 06 '23 at 20:53
  • 1
    Interestingly: `1 == True` is also true. I am curious in this case, my understanding is that `True` is "up-casted" to an `int` value, then compared. So that `1 == 1` returns true. Anyone feel free to correct me if i'm misunderstanding it. – rv.kvetch Feb 06 '23 at 20:54
  • 1
    Booleans don't need to be "upcasted," they are literally integers in Python. – kindall Feb 06 '23 at 21:40

0 Answers0