[Python-Dev] switch statement
mike at selik.org
Fri Sep 21 14:40:10 EDT 2018
First, this sounds like it belongs on python-ideas, not python-dev.
Second, when you do send a message to python-ideas, it'll help to
accompany it with a realistic example usage that motivates your
On Fri, Sep 21, 2018 at 11:18 AM <e2qb2a44f at prolan-power.hu> wrote:
> 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.)
> Python-Dev mailing list
> Python-Dev at python.org
> Unsubscribe: https://mail.python.org/mailman/options/python-dev/mike%40selik.org
More information about the Python-Dev