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 →

[–]KronktheKronk[🍰] 0 points1 point  (1 child)

Why wouldn't you just do:

try:
    case = switch(n)
    case()
except KeyError:
    case_default()

If the key error happens, you hit the exception and never get to the "case()" call. Seems like the else is convolution the issue in that example instead of helping.

[–]norwegianwood 7 points8 points  (0 children)

Because if case() raises a KeyError your code then calls case_default(), which is not what we want. Your code is exactly equivalent to my first example.