[Python-Dev] PEP 3103: A Switch/Case Statement
rrr at ronadam.com
Wed Jun 28 00:06:52 CEST 2006
Guido van Rossum wrote:
>> What was intended probably would be more closely related to constructing
>> a switch with BASICS gosub command.
> I understand now.
> But I have a question: if I write
> for i in range(10):
> switch S:
> case i: print 42
> (i.e. the switch is *inside* the for loop) does the switch get defined
> 10 times (with 10 different case values!) or not?
In this instance the switch would be redefined 10 times. The ending
switch would be:
case 10: print 42
The static keyword could be used with this form as well to force define
time evaluation. (see last example.)
> I'm not sure I like the idea of using BASIC as a way to explain Python
> functionality... :-)
Yes, I agree! :-)
Fortunately, once (and if) it's defined (what ever it turns out to be)
Python examples can be used to explain Python. ;-)
>> Each case label expression would be evaluated when the switch block is
>> executed, ie... in order it appears in the program, but the code for
>> each case would be skipped until a (do choice in choices) line. Each
>> switch case block would not fall through but return to the next line
>> after the 'do' line by default.
>> The whole thing could be put in a separate function or method if it's
>> desired to get the single function call form you suggested along with a
>> separate name space.
>> def switcher(choice):
>> switcher roo:
>> 1: a = 42
>> 42: a = 1
>> else: raise ValueError
>> do choice in switcher:
>> return a
>> switcher(1) -> 42
>> switcher(42) -> 1
>> switcher(100) -> raises exception
> I'm still unclear on when you propose the case expressions to be
> evaluated. Each time the "switch" statement is encountered? That would
> be the most natural given the rest of your explanation. But then a
> switch inside a function that references globally defined constants
> would be re-evalulated each time the function is called; much of the
> discussion here is focused on trying to reduce the number of times the
> switch cases are evaluated to once per program invocation or once per
> function *definition*.
Each time the 'switch' statement is encountered for the above.
Allowing static to be used with this form could be an option to force
function define time evaluations of cases.
ONE = 1
FOURTYTWO = 42
static switcher roo: # evaluate cases at function def time.
ONE: a = 42
FOURTYTWO: a = 1
else: raise ValueError
do choice in switcher:
switcher(1) -> 42
switcher(42) -> 1
switcher(100) -> raises exception
That would give you both call time and def time evaluations for cases
with clear behavior for both (I think).
Also since the switch has a name, it might be possible to examine their
values with dir(switch_suite_name). That might help in debugging and
explaining the behavior in different situations.
The 'case' keyword could be left off in this form because the switch
body is always a suite. I find it more readable without it in the case
of simple literals or named values, and more readable with it for more
I don't think I can clarify this further without getting over my head.
I probably am a bit already. I'm in the know enough to get myself in
trouble (at times) group. ;-)
More information about the Python-Dev