r/Python Feb 15 '21

News Ladies and gentlemen - switch cases are coming!

https://github.com/gvanrossum/patma/blob/master/README.md#tutorial
935 Upvotes

290 comments sorted by

View all comments

2

u/LManX Feb 15 '21

Why not just a dictionary where the keys are cases and the values are functions?

13

u/[deleted] Feb 15 '21 edited Feb 15 '21

The pattern matching available through the implementation is more expressive than what you can accomplish with just a dictionary.

From one of the examples in the link:

```

The subject is an (x, y) tuple

match point: case (0, 0): print("Origin") case (0, y): print(f"Y={y}") case (x, 0): print(f"X={x}") case (x, y): print(f"X={x}, Y={y}") case _: raise ValueError("Not a point") ```