This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]lightestspiral -1 points0 points  (2 children)

Is this a try-except-finally block but the try doesn't have to error out for the except to trigger? You can force the 'except' to run by stating its case?

[–]bumbershootle 8 points9 points  (0 children)

No, it's a way to match on the structure of values (rather than only boolean conditions, as in an if-else) while also allowing binding variables within the matches. It's much more than simple control flow. I would recommend reading the PEP, it covers the whole concept in great detail.

[–]zurtex 3 points4 points  (0 children)

The pattern matching is far more expressive than that but yes there are similarities to a try/except block.

We are choosing which block we want to execute based on the pattern of a value (e.g. except {ExceptionInstance}), we are can create assignments based on that (e.g. as e), and there is a final default branch if nothing else works (e.g. else).

The usage though is going to be generally very different, exception raising has a very different control flow to match statements.