[Python-Dev] Multiple expression eval in compound if statement?
Raymond Hettinger
python at rcn.com
Mon Jun 13 04:20:31 CEST 2005
> I'm horsing around with recognizing switch-like if statements like:
>
> if x == 1:
> print 1
> elif x == 2:
> print 2
> else:
> print "unknown"
>
> in the compiler and generating O(1) code. "x" can be any expression,
but
> must be precisely the same in each elif clause, the comparison
operator
> must
> be "==" and the RHS of the test must evaluate to a simple hashable
> constant
> (string, float, integer - still working on None, constant tuples may
be
> allowed later). I can currently recognize such constructs and am
working
> on
> code generation.
I think it unwise to allow x to be any expression. Besides altering
existing semantics, it leads to code redundancy and to a fragile
construct (where the slightest alteration of any of the expressions
triggers a silent reversion to O(n) behavior).
Raymond
More information about the Python-Dev
mailing list