
Most of programming languages out there have switch-case statements. Python doesn't have support for switch-case statements though. switch-case statements can sometimes be nice and tidy and sometimes it may be a horrible nightmare. But that also applies to if-elif-else statements. It would be nice to have switch-case statements support in python. Suggested syntax :- ------------------- switch expr: case expr2: case expr3: statements default: statements How should it be implemented? ----------------------------------- First of all this will require a new syntax. Second I suggest for comparing the case expr with switch expr, compare the pointers instead of objects for better performance. Usually if the ladder is represented using if-elif-else logic then it would be lower performance because we'll end up comparing objects not pointers.

How is this differs from PEP 634 (https://docs.python.org/3.10/whatsnew/3.10.html#pep-634-structural-pattern-m... On 2021. 05. 04. 20:33, Shreyan Avigyan wrote:

Isn't PEP 643 for PEG grammar pattern matching? (I'm talking about switch statements in Python)

Isn't PEP 643 for PEG grammar pattern matching? (I'm talking about switch statements in Python)
I think you are referring to PEP 634, the one I linked to. I suggest you read it (and the other link I sent). I copy from PEP 636, marked as a sort of "tutorial" for PEP 634: ``` A match statement takes an expression and compares its value to successive patterns given as one or more case blocks. This is superficially similar to a switch statement in C, Java or JavaScript (and many other languages), but much more powerful. The simplest form compares a subject value against one or more literals: def http_error(status): match status: case 400: return "Bad request" case 404: return "Not found" case 418: return "I'm a teapot" case _: return "Something's wrong with the Internet" ``` - DLD

Ok. Now I'm able to understand. PEP 634 should have a reference to PEP 636 or else it's pretty hard to understand because the syntax section demonstrates PEG implementation of this.

See the pattern-matching PEPs such as https://www.python.org/dev/peps/pep-0634/ In short, python soon will have a sort of switch statement, though the semantics will be a bit different and it is actually a form of pattern matching. See also the notes on pattern mathing in https://docs.python.org/3.10/whatsnew/3.10.html - DLD

This thread is closed therefore. (It was mistakenly opened due to misunderstanding.)

How is this differs from PEP 634 (https://docs.python.org/3.10/whatsnew/3.10.html#pep-634-structural-pattern-m... On 2021. 05. 04. 20:33, Shreyan Avigyan wrote:

Isn't PEP 643 for PEG grammar pattern matching? (I'm talking about switch statements in Python)

Isn't PEP 643 for PEG grammar pattern matching? (I'm talking about switch statements in Python)
I think you are referring to PEP 634, the one I linked to. I suggest you read it (and the other link I sent). I copy from PEP 636, marked as a sort of "tutorial" for PEP 634: ``` A match statement takes an expression and compares its value to successive patterns given as one or more case blocks. This is superficially similar to a switch statement in C, Java or JavaScript (and many other languages), but much more powerful. The simplest form compares a subject value against one or more literals: def http_error(status): match status: case 400: return "Bad request" case 404: return "Not found" case 418: return "I'm a teapot" case _: return "Something's wrong with the Internet" ``` - DLD

Ok. Now I'm able to understand. PEP 634 should have a reference to PEP 636 or else it's pretty hard to understand because the syntax section demonstrates PEG implementation of this.

See the pattern-matching PEPs such as https://www.python.org/dev/peps/pep-0634/ In short, python soon will have a sort of switch statement, though the semantics will be a bit different and it is actually a form of pattern matching. See also the notes on pattern mathing in https://docs.python.org/3.10/whatsnew/3.10.html - DLD

This thread is closed therefore. (It was mistakenly opened due to misunderstanding.)
participants (5)
-
Antal Gábor
-
Brandt Bucher
-
Daniel Moisset
-
David Lowry-Duda
-
Shreyan Avigyan