[Python-Dev] Switch statement (original) (raw)
M.-A. Lemburg mal at egenix.com
Fri Jun 16 10:20:20 CEST 2006
- Previous message: [Python-Dev] Switch statement
- Next message: [Python-Dev] Switch statement
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Raymond Hettinger wrote:
The optimisation of the if-elif case would then simply be to say that the compiler can recognise if-elif chains like the one above where the RHS of the comparisons are all hashable literals and collapse them to switch statements.
Right (constants are usually hashable :-). The LHS is more challenging. Given: if x == 1: pone() elif x == 2: ptwo() elif x == 3: pthree() else: pcatchall() There is no guarantee that x is hashable. For example: class X: def init(self, value): self.value = value def cmp(self, y): return self.value == y x = X(2)
That's a good point.
The PEP already addresses this by retricting the type of x to a few builtin immutable and hashable types:
...the switching variable is one of the builtin
immutable types: int, float, string, unicode, etc. (not
subtypes, since it's not clear whether these are still
immutable or not).
-- Marc-Andre Lemburg eGenix.com
Professional Python Services directly from the Source (#1, Jun 16 2006)
Python/Zope Consulting and Support ... http://www.egenix.com/ mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,FreeBSD for free ! ::::
- Previous message: [Python-Dev] Switch statement
- Next message: [Python-Dev] Switch statement
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]