[Python-3000] Switch/Case statement PEP
Thomas Dixon
reikonmusha at gmail.com
Sat Dec 2 19:56:49 CET 2006
I was reading the PEP regarding the Switch/Case statement (http://
www.python.org/dev/peps/pep-3103/), and have a few thoughts about it.
1) Proposed layout
Here is how I propose the 'switch' statement to be constructed.
Further on I will give the reasoning behind the syntax. As you can
see, I am also a fan of Alternative 1.
case EXPR:
on EXPR:
SUITE
on EXPR:
SUITE
continue
else:
SUITE
2) Statement Keywords
You will notice 'switch' was replaced with what is typically found
in the body of the switch statement, 'case'. Not only is it shorter,
but I feel it also better describes what the statement actually does.
You'll also notice that the 'case' keyword in the body was replaced
with 'on'. To me, it just sounds natural. Using 'if' as some others
have proposed may end up being confusing, and 'case' wasn't used in
the body for obvious reasons.
3) Control flow
The lone 'continue' in the example is indicative of another one of
my proposals. In switch statements the 'break' used in most languages
is almost completely redundant, and thus should be implemented by
default. I also propose that the switch statement allows use of
Python's loop control flow keywords. Which means that by default the
flow doesn't 'fall through' in the absence of the 'break', but that
one can force it to do so by using 'continue', as you might expect it
to behave. Below I have proposed Python examples and their C
equivalents to hopefully better explain what I mean.
-- Example 1:
Python:
case EXPR:
on EXPR:
SUITE
C:
switch (EXPR) {
case EXPR:
SUITE
break;
}
-- Example 2:
Python:
case EXPR:
on EXPR:
SUITE
continue
C:
switch (EXPR) {
case EXPR:
SUITE
}
I hope I explained myself well enough, and that I haven't taken up
too much of your time.
Have a nice day,
Thomas Dixon
reikonmusha at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-3000/attachments/20061202/4cc30a56/attachment.html
More information about the Python-3000
mailing list