By: Alex Comerford (alexanderjcomerford@gmail.com)
To read the original readme for cpython, build instructions, and more go to README.rst
This fork of cpython
adds two new statements to the python core language, switch
and case
. Requisite changes have been made to the python grammar, asdl, and AST compiler to support execution of these statements.
The new syntax for these statements is
switch EXPR:
case EXPR:
SUITE
case EXPR:
SUITE
else:
SUITE
Sample code
var = 1
switch var:
case 0:
print("it's zero!")
case 1:
print("it's one!")
else:
print("it's something else!")
evaluates to
it's one!
There have been previous PEP's proposing switching on multiple values here and here which have been subsequently denied in favor of using if/elif chains. However switch
case
can be extremely handy in some situations in which if/elif chains might feel a little verbose.
Example:
switch input(">"): x = input(">")
case 'hello': if x == 'hello':
SUITE SUITE
case 'bye': elif x == 'bye':
SUITE SUITE
case 'how are you': elif x == 'how are you':
SUITE SUITE
else: else:
SUITE SUITE
Instead of including a variable reference and a ==
operator, both are implicitely included in switch
case
. While bytecode is slightly different in both implemenations, both produce the same output in slightly different code styles.
Using switch
and case
as the statements of choice were selected from several proposed syntax variants in PEP 275. switch
and case
were chosen because it seems the most familiar compared to other languages.
Follow the build instructions in README.rst and run the resulting executable python
.
or simply
./configure
make