1

Why is

match type('a'):
    case list:
        print('a list')
    case str:
        print('a string')

wrong with giving an error "SyntaxError: name capture 'list' makes remaining patterns unreachable"

while type('a')==str is true?

Fred
  • 417
  • 5
  • 16
  • @Chris while those answers give alternatives they don't address the source of the error. Try: [Capture makes remaining patterns unreachable](https://stackoverflow.com/questions/67525257/capture-makes-remaining-patterns-unreachable/67525259#67525259) Python's match is different from the switch statement in other languages like C/C++. Here list and str are treated as variables for the name capture pattern. Since list can always be assigned to type('a') it is irrefutable making the case True. But, an irrefutable case can only be the last case, thus the error. – DarrylG Dec 16 '22 at 19:24
  • @DarrylG, well Barmar's answer says, "A simple name like `int` is a pattern that matches anything, and sets `int` to the value", but I agree that Raymond's answer on the other question is better. I'll make it the primary dupe. Thanks. – ChrisGPT was on strike Dec 16 '22 at 19:35
  • Thanks for the advice. The logic, though, is not intuitive. – Fred Dec 17 '22 at 13:10

0 Answers0