News for hackers. Stuff that matters.
Re:Structural pattern matching in Python 3.10 by benhoyt on Monday September 20, 2021 @09:42PM (#28601187)
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')

There are no comments on this comment yet.