[Python-Dev] Switch statement

Phillip J. Eby pje at telecommunity.com
Sat Jun 24 19:18:23 CEST 2006

At 07:04 PM 6/24/2006 +0200, Fredrik Lundh wrote:
>I don't see this as much of a problem, really: we can simply restrict
>the optimization to well-known data types ("homogenous" switches using
>integers or strings should cover 99.9% of all practical cases), and then
>add an opcode that checks uses a separate dispatch object to check if
>fast dispatch is possible, and place that before an ordinary if/elif

What about switches on types?  Things like XML-RPC and JSON want to be able 
to have a fast switch on an object's type and fall back to slower tests 
only for non-common cases.  For that matter, you can build an effective 
multiway isinstance() check using something like:

     for t in obtype.__mro__:
         switch t:
         case int: ...; break
         case str: ...; break
         # not a recognized type

This is essentially what RuleDispatch does in generic functions' dispatch 
trees now, albeit without the benefit of a "switch" statement or opcode.

More information about the Python-Dev mailing list