2 ms·
Indeed, I've also used this pattern. I've found that I don't like using it. As you write, it's "inadequate in some situations", which I consider as part of the
by eesmith 7y ago
Indeed, I've also used this pattern.
I've found that I don't like using it. As you write, it's "inadequate in some situations", which I consider as part of the cumbersome complexities I mentioned.
For examples, do_stuff() and do_other_stuff() may need to share variables, and do_something_else() might need the line number to report an error while the others don't.
This can be handled with shared state/nonlocal, and by passing in more parameters to the generic handler API, but these add complexity.
Or, different parts of the file may have different line dispatch processing (eg, a header block followed by a data block followed by a footer block) where one of the handlers must indicate the transition to a different processor.
Also, function dispatch in CPython is slow. While the regex tests are also slow, it can also be important to consider the (using a hypothetical number) 5% overhead for dispatching over inline code.