FW: Switch statements again

Jeff Hinrichs jlh at cox.net
Wed Jan 15 23:05:51 EST 2003


"Skip Montanaro" <skip at pobox.com> wrote in message
news:mailman.1042684109.16197.python-list at python.org...
>
>     >> If/elif/else remains the most common method.  If Python ever gains
>     >> something like a switch statement you can bet the farm it won't
have
>     >> a "fall thru" feature though, so the way you've coded your C switch
>     >> statement wouldn't work.
>
>     Steven> but the absolute /only/ reason you'd use a switch over an if
>     Steven> (besides looking better) is fall through.
>
> Well, there is the efficiency aspect.  Take a look at eval_frame in
Python's
> ceval.c.  It's main switch statement has 109 case labels.  Since switch is
> implemented as essentially a computed goto, you can get to any branch in
the
> same amount of time.  With if/then/else you'd have to be very careful to
get
> your options in the right order.
+1 on switch, +1 on no fall-through
-Jeff






More information about the Python-list mailing list