News for hackers. Stuff that matters.
Re:What Is the Switch-Case Equivalent in Python? by diarrhea on Sunday April 04, 2021 @02:34AM (#26687370)
Not the parent commenter, but I use that pattern all the time, mainly to match CLI flags/args/options to an action.

Say you have a program like "docker-compose". It has multiple subcommands. Each subcommand is a dict string key and the appropriate action to take is its value, a callable. This also plays well with argparse's parser "choices" argument.

The by far biggest downside is then that all callables mustn't take arguments, or take all the same ones. You cannot dispatch to a callable and dynamically assign what arguments to call it with without losing the prior benefits. Then it gets awkward (like having to throw around args and kwargs all over the place). But up to that point, I find the pattern useful in this case.

There are no comments on this comment yet.