[Python-Dev] switch statement

e2qb2a44f at prolan-power.hu e2qb2a44f at prolan-power.hu
Fri Sep 21 14:02:32 EDT 2018


A humble proposal for a switch-like statement syntax for Python:

- - -
switch blah in (100, 2, 30, 'bumm'):
  x = 88
case blah in (44, 55):
case blah in (8):
- - -

So, let's use, and allow only *tuples*.
As early as possible, build a jump table, based on (foreknown) small integer values. As in other languages.
Strings may have to be hashed (in "compile time"), to obtain small integer value. Some secondary checking may
have to be done for exact content equality. (Alternative: do no allow strings at all.)
For gaps in the integer range: maybe apply some very basic dividing/shifting to "compact" the range. (As
compilers optimize in other languages, I guess -- but I may be totally wrong.) (For example find "unused bits"
in the numbers (in 2-base representation). newnum = orignum >> 3 & 6 | orignum & ~6. newnum is smaller (roughly
1/8) than orignum.)
The (achievable) goal is to be faster than hash table lookup. (A hash table with keys 100, 2, 30, 'bumm' etc.)
And approach the speed of single array-index lookup. (Or even faster in some cases as there will be just jumps
instead of calls?)
(I am not an "expert"!)

Let allow fallthrough or not? - To be decided. (Either is compatible with the above.)

I know about PEP 3103 and

(I do not know how to comment on a PEP, where to discuss a PEP. If this is inappropriate place, please forward it.)


More information about the Python-Dev mailing list