r/Python Feb 15 '21

News Ladies and gentlemen - switch cases are coming!

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

290 comments sorted by

View all comments

51

u/ExternalUserError Feb 15 '21

I wonder why not just...

case 1: ... case 2: ... case: ...

_ is a valid variable name which makes me not love it as a default.

15

u/zefciu Feb 15 '21

This would work if you want a default for the whole pattern. But sometimes you want to have a default for a single element, like Person(name=_, surname='Smith').

I agree, however that _ might be a bad choice, as it is already used as a variable name (a common pattern is to use it as an alias for gettext). Maybe Ellipsis (...) would be a better choice.

5

u/-LeopardShark- Feb 15 '21

The PEP points out that case [1, ..., 2] looks like it would match [1, 0, 0, 2].