FW: Switch statements again

Skip Montanaro skip at pobox.com
Thu Jan 16 02:18:17 CET 2003


    >> 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.

Skip





More information about the Python-list mailing list