[Python-Dev] Alternatives to switch?

Fredrik Lundh fredrik at pythonware.com
Sun Jun 25 21:14:57 CEST 2006

Talin wrote:

> In fact, I'd like to point out something that hasn't been brought up, 
> which is that in many cases having a closure rebind the switch cases 
> defeats the purpose of the thing. For example:
>     def outer():
>        def inner(x):
>           switch(x):
>           case 1: ...
>           case 2: ...
>           case 3: ...
>        return inner
> If the switch cases are bound at the time that 'inner' is defined, it 
> means that the hash table will be rebuilt each time 'outer' is called. 

not if all cases are literals, as in your example.

and how common is this, really?  are you sure you're not arguing against 
a construct that is more efficient in many existing use cases, based on 
a single, mostly hypothetical use case ?

(after all, if you don't want Python to waste time optimizing things for 
you, you don't have to use a construct that does that...)


More information about the Python-Dev mailing list