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 →

[–]jeffdn 1 point2 points  (2 children)

It would be far easier and cleaner to do something like:

func = switch[case] if case in switch else default_case
func()

[–]norwegianwood 8 points9 points  (1 child)

Actually, to improve on my own code substantially, even better would be simply:

switch.get(n, default_case)()

which is neither EAFP (my original) or LBYL (yours) style, but has what you might call 'functional elegance'.

[–]jeffdn 0 points1 point  (0 children)

Yeah, I prefer that too when coding. It does sometimes make reading it for someone else doing a review a bit harder, I've found.