Is the new case statement crippled

In my mind this should work. It provides a nice syntax for a multiway branch making it easy to re-arrange the options and works in other languages.

Putting True in brackets allows the match statement to pass the syntax check but this does not work with the cases.


#  match syntax check
def stry(x):
    match True :
        case x == 5 :
           print ( 'x is 5' ) 
        case x == 6 :
           print ( 'x is 6 ' ) 
        case _ :
           print ( 'case else' ) 
    print('fini' )
    return    
for x in (5,6,7) :
    stry(x)

The case statement does not accept normal expressions. Someone more familiar with the match statement can tell you more details but I am wondering why you want to replace if-elif-else with match-case. The code implemented using normal if is shorter, more clear and less indented:

def stry(x):
    if x == 5:
       print('x is 5')
    elif x == 6:
       print('x is 6')
    else:
       print('case else')
    print('fini')

for x in (5, 6, 7):
    stry(x)

Python’s match statement is not a C switch statement. If all you want is to test a sequence of conditions, use if...elif...else.

Have you read the match PEP’s tutorial?

The match statement is much more powerful than a switch statement, but it is also quite complicated and takes some getting used to.

Good luck!