Hi, A humble proposal for a switch-like statement syntax for Python: - - - switch blah in (100, 2, 30, 'bumm'): dosomething1() x = 88 case blah in (44, 55): otherstuff(9) case blah in (8): boo() else: wawa() - - - 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 https://docs.python.org/3.8/faq/design.html?highlight=switch#why-isn-t-there... (I do not know how to comment on a PEP, where to discuss a PEP. If this is inappropriate place, please forward it.) --
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 proposal. On Fri, Sep 21, 2018 at 11:18 AM <e2qb2a44f@prolan-power.hu> wrote:
Hi,
A humble proposal for a switch-like statement syntax for Python:
- - - switch blah in (100, 2, 30, 'bumm'): dosomething1() x = 88 case blah in (44, 55): otherstuff(9) case blah in (8): boo() else: wawa() - - -
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 https://docs.python.org/3.8/faq/design.html?highlight=switch#why-isn-t-there...
(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@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/mike%40selik.org
As Michael said, this belongs on python-ideas, but it's interesting. I'd support it, though my input in that regard is worth approximately $0.00. ;) It's the core devs and especially the eventual BDFL replacement whom you would have to convince. Without getting into an extended discussion here on python-dev, I'd like to offer one brief comment to try to help improve the proposal: On Fri, Sep 21, 2018 at 2:17 PM <e2qb2a44f@prolan-power.hu> wrote:
Let allow fallthrough or not? - To be decided. (Either is compatible with the above.)
I would argue for non-fallthrough as the default with support for explicitly requesting fallthrough when desired by using "continue". I don't know of any prior art for doing it that way, but it makes sense to me. It eliminates hidden bugs from missing a "break" while still allowing it when needed. Good luck with the proposal!
There's already a rejected PEP about a switch statement: https://www.python.org/dev/peps/pep-3103/. There's no point bringing this up again unless you have a new use case. There have been several promising posts to python-ideas about the much more powerful idea of a "match" statement. Please search for those before re-posting on python-ideas. -- --Guido van Rossum (python.org/~guido)
There have been several promising posts to python-ideas about the much more powerful idea of a "match" statement
I actually even started working on a PEP about this (pattern matching), but then decided to postpone it because it is unlikely that anything of this size can be discussed/accepted in current situation. We can return back to the idea when decision-making model will clarify. -- Ivan On Fri, 21 Sep 2018 at 22:12, Guido van Rossum <guido@python.org> wrote:
There's already a rejected PEP about a switch statement: https://www.python.org/dev/peps/pep-3103/. There's no point bringing this up again unless you have a new use case.
There have been several promising posts to python-ideas about the much more powerful idea of a "match" statement. Please search for those before re-posting on python-ideas.
-- --Guido van Rossum (python.org/~guido) _______________________________________________ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/levkivskyi%40gmail.com
On Fri, Sep 21, 2018 at 02:10:00PM -0700, Guido van Rossum wrote:
There's already a rejected PEP about a switch statement: https://www.python.org/dev/peps/pep-3103/. There's no point bringing this up again unless you have a new use case.
There have been several promising posts to python-ideas about the much more powerful idea of a "match" statement. Please search for those before re-posting on python-ideas.
The Coconut transpiler also includes some interesting ideas for a match and case statement: http://coconut-lang.org/ https://coconut.readthedocs.io/en/master/DOCS.html#match https://coconut.readthedocs.io/en/master/DOCS.html#case -- Steve
participants (6)
-
e2qb2a44f@prolan-power.hu
-
Guido van Rossum
-
Ivan Levkivskyi
-
Jonathan Goble
-
Michael Selik
-
Steven D'Aprano