[Python-Dev] Re: switch statement

M.-A. Lemburg mal at egenix.com
Mon Apr 25 10:46:28 CEST 2005

Shannon -jj Behrens wrote:
> On 4/20/05, M.-A. Lemburg <mal at 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
>>>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 ?

Marc-Andre Lemburg

Professional Python Services directly from the Source  (#1, Apr 25 2005)
>>> Python/Zope Consulting and Support ...        http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/

::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,FreeBSD for free ! ::::

More information about the Python-Dev mailing list