
On 4/25/05, M.-A. Lemburg <mal@egenix.com> wrote:
Shannon -jj Behrens wrote:
On 4/20/05, M.-A. Lemburg <mal@egenix.com> wrote:
Fredrik Lundh wrote:
PS. a side effect of the for-in pattern is that I'm beginning to feel that Python might need a nice "switch" statement based on dictionary lookups, so I can replace multiple callbacks with a single loop body, without writing too many if/elif clauses.
PEP 275 anyone ? (http://www.python.org/peps/pep-0275.html)
My use case for switch is that of a parser switching on tokens.
mxTextTools applications would greatly benefit from being able to branch on tokens quickly. Currently, there's only callbacks, dict-to-method branching or long if-elif-elif-...-elif-else.
I think "match" from Ocaml would be a much nicer addition to Python than "switch" from C.
PEP 275 is about branching based on dictionary lookups which is somewhat different than pattern matching - for which we already have lots and lots of different tools.
The motivation behind the switch statement idea is that of interpreting the multi-state outcome of some analysis that you perform on data. The main benefit is avoiding Python function calls which are very slow compared to branching to inlined Python code.
Having a simple switch statement would enable writing very fast parsers in Python - you'd let one of the existing tokenizers such as mxTextTools, re or one of the xml libs create the token input data and then work on the result using a switch statement.
Instead of having one function call per token, you'd only have a single dict lookup.
BTW, has anyone in this thread actually read the PEP 275 ?
I'll admit that I haven't because dict-based lookups aren't as interesting to me as an Ocaml-style match statement. Furthermore, the argument "Instead of having one function call per token, you'd only have a single dict lookup" isn't very compelling to me personally, because I don't have such a performance problem in my applications, which isn't to say that it isn't important or that you don't have a valid point. Best Regards, -jj -- I have decided to switch to Gmail, but messages to my Yahoo account will still get through.