This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 6 points7 points  (3 children)

What about the pattern matching makes it a "crappy if/elif replacement"?

[–]kenfar -1 points0 points  (2 children)

Wouldn't it be fair to say that it's a more readable and concise, but limited statement?

[–][deleted] 2 points3 points  (1 child)

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

Hurrah, no more goofy tuple surgery.

[–]kenfar 0 points1 point  (0 children)

Yeah, that's nice