3 ms·
Do you mean like "f = handlers.get(args.command, error); f(args)"? It's a fine pattern (of course, it's only a tiny fraction of what pattern matching can do). H
by benhoyt 5y ago
Do you mean like "f = handlers.get(args.command, error); f(args)"? It's a fine pattern (of course, it's only a tiny fraction of what pattern matching can do). However, it does mean that if the handlers aren't already functions, you have to convert them to (possibly nested) functions, but that can sometimes make the code clearer and easier to test anyway.
But if you're not matching structure I really don't see what's wrong with plain old if...elif? And you can easily add more complex tests like "in" clauses this way too:
cmd = args.command
if cmd == 'push':
# do push
elif cmd in ['pull', 'pul']:
# do pull
else:
error(f'command {cmd!r} not yet implemented')