match page_slug:
case 'status':
return render('status.html')
case 'about':
return render('about.html')
case 'contact':
return render('contact.html')
case _:
return render('home.html')
instead of
if page_slug == 'status':
return render('status.html')
if page_slug == 'about':
return render('about.html')
if page_slug == 'contact':
return render('contact.html')
return render('home.html')
Reduced visual noise. Rather than worrying about what all those conditionals are and the repetition of the first few pieces, I just know they're all checking equality and I don't have to type my variable name a dozen times.
It resembles with statements, in my opinion, which is nicer. It’s like with case match foo. It just looks clean and easy on the eyes.
It’s also not the only use for case statements either, the main advantage as I read about it was handling not only different values in a variable, but different configurations of variables entirely. Like if foo can be a string, a 2-item tuple or a 5-item tuple. case makes that easier to deal with in a pythonic way.
Exercise for the reader - go examine the implementation of match in cpython and reflect on whether it's implemented like switch/case in statically typed languages or... the match statement in PHP.
Match seems every bit as powerful as if to me. It's not limited to just checking if two values are equal.
For example, if you're writing a flask view decorator, you need to check to see if it's a response object, a single value, or a tuple of (rv, status) or (rv, headers), or (rv, headers, status) - I think (rv, status, headers) is supported as well. So you end with something goofy as hell like:
if isinstance(rv, Response): # handle response
if not isinstance(rv, tuple):
rv = (rv,)
rv, headers, status = (rv + (None, None)[:3]
Whereas with match, you could do:
match rv:
case Response():
# handle response object
case (response, int(status)):
# rv + status
case (response, dict(headers)):
# rv + headers
case (response, int(status), dict(headers)):
# you get the idea
case _:
# just the response value
-26
u/[deleted] Mar 19 '21
Tell it I hate it. Taking away very useful identifiers like 'match' and 'case' to use as a crappy if/elif replacement.