[Python-Dev] switch-based programming in Python (original) (raw)

M.-A. Lemburg mal@lemburg.com
Thu, 08 Nov 2001 09:50:59 +0100


"Martin v. Loewis" wrote:

> Wouldn't it make sense to enable the byte code compiler to take the > above construct and turn it into a dictionary based switch statement > ? That won't work. You cannot know what type "x" has, so you don't know in advance how "x == 'one'" is evaluated.

But you do know that x won't change from one compare to the next, so a single dictionary lookup could replace the equality tests (provided that x is hashable).

As mentioned in the posting: the compiler probably has to be given some extra information to enable this sort of optimization. I'm not sure how the information could be "encoded", though. Suggestions are appreciated, as always :-)

What do you think about the general idea, BTW ?

-- Marc-Andre Lemburg CEO eGenix.com Software GmbH


Consulting & Company: http://www.egenix.com/ Python Software: http://www.lemburg.com/python/