[Python-Dev] switch-based programming in Python

M.-A. Lemburg mal@lemburg.com
Wed, 07 Nov 2001 16:23:32 +0100


Now I know that this has been brought quite a few times in the
past. Still, with the slowness of Python method calls, a
switch-based coding style would be nice way to implement fast 
token based processing of data in Python rather than C.

Currently, dispatching of execution based on the value of
one variable is usually implemented by having some dictionary
of possible values and then calling method which implement the
different branches of execution. This works well for code which
uses medium sized methods, but fails badly for small ones such
as code which is often used in method callback based parsers.

The alternative is using lengthy 

	if x == 'one': ...
        elif x == 'two': ...
        elif x == 'three': ...
        else: ...default case...

constructs.

Wouldn't it make sense to enable the byte code compiler to take 
the above construct and turn it into a dictionary based 
switch statement ?

I'm not talking about adding syntax to the language, it would
just be nice to have the compiler recognize this kind of code
(somehow; perhaps with some extra help) and produce optimized
code for it, possibly using new opcodes for the switching 
operation.

Thoughts ?

-- 
Marc-Andre Lemburg
CEO eGenix.com Software GmbH
______________________________________________________________________
Consulting & Company:                           http://www.egenix.com/
Python Software:                        http://www.lemburg.com/python/