[Python-Dev] switch-based programming in Python

Martin v. Loewis martin@v.loewis.de
Thu, 8 Nov 2001 16:55:16 +0100


> I don't think you need anything extra if the RHS of the == is a hashable
> literal of some sort and the LHS is always the same simple variable or
> subscript expression.  If the compiler can recognize the structure (that may
> be a big "if"), all you need is a dictionary of offsets stored in the
> function's constants.  You just execute the equivalent of
> 
>     offset = jumptable.get(x, E)

That won't work: It maybe that x is not hashable, even though it
compares equal with the RHS values.

Even if it was hashable, you'd change the language semantics: In the
original code, you call __cmp__, say, 20 times; in the modified code,
you call __hash__ once and __cmp__ perhaps also once. If __cmp__ has
side effects, you get a language change.

Regards,
Martin