News for hackers. Stuff that matters.
Re:Pattern Matching in Python by ggm on Tuesday October 20, 2020 @05:58PM (#24842755)
Always wanted a switch coding style. Now, offered one i am having second thoughts introspecting if my code improves for readability if I use one. I think on balance yes. I've always like this approach to a single value or expression gating choices.

Also wondering about the degenerate forms of fall through alternates, so it's a stack of code optionally run.

There is another link to c code switches and duffs device in hn recently

Re:Pattern Matching in Python by pmart123 on Wednesday October 21, 2020 @09:49AM (#24847849)
Switch statements can work well from a readability standpoint such as the HTTP error example they use. However, I almost feel it is more Pythonic to use a dictionary instead, collapsing the function into:

def http_error(status):

  try:
    return HTTP_STATUS_CODES[status]
  except KeyError:
    return "Something else"
Also, method polymorphism always seemed more powerful and intuitive to me than switch statements when the operation is something like:

  multiply(scalar, scalar)
  multiply(vector, scalar)
  multiply(matrix, scalar)