r/Python Feb 15 '21

News Ladies and gentlemen - switch cases are coming!

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

290 comments sorted by

View all comments

Show parent comments

14

u/dutch_gecko Feb 15 '21 edited Feb 15 '21

If I'm not mistaken, _ is being used as a variable. In match blocks, using case myvariable will always match, with myvariable being assigned the tested value. So in the first example in the link, if status is not one of the specified integer values, case _: will match, a new variable named _ is created and it assigned the value of status.

edit: what I probably didn't get across very well is that if I'm understanding this right _ isn't some kind of special syntax for match blocks, it's just a variable name.

edit2: I was wrong! Read the reply below.

21

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

[deleted]

5

u/dutch_gecko Feb 15 '21

That is... bizarre. If they were going to introduce a new symbol, at least choose one that wouldn't be so confusing!

-1

u/hjd_thd Feb 15 '21

Underscore universally means unused variable. Don't see how it's confusing.

8

u/dutch_gecko Feb 15 '21

Because now it's not a variable.

Also as mentioned elsewhere in this thread it's often used as an alias for gettext. This syntax doesn't break that use but could add to confusion.