[Python-Dev] Switch statement
Guido van Rossum
guido at python.org
Wed Jun 21 18:47:21 CEST 2006
On 6/21/06, Fredrik Lundh <fredrik at pythonware.com> wrote:
> Guido van Rossum wrote:
>
> > (Note how I've switched to the switch-for-efficiency camp, since it
> > seems better to have clear semantics and a clear reason for the syntax
> > to be different from if/elif chains.)
>
> if you're now in the efficiency camp, why not just solve this on the
> code generator level ? given
>
> var = some expression
> if var == constant:
> ...
> elif var == constant:
> ...
>
> let the compiler use a dispatch table, if it can and wants to.
But in most cases the 'constant' is actually an expression involving a
global, often even a global in another module. (E.g. sre_compile.py)
The compiler will have a hard time proving that this is really a
constant, so it won't optimize the code.
The proposed switch semantics (create the table when the containing
function is defined) get around this by "defining" what it means by
"constant".
BTW I would like references to locals shadowing globals to be flagged
as errors (or at least warnings) so that users who deduced the wrong
mental model for a switch statement are caught out sooner.
--
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-Dev
mailing list