[Python-Dev] Switch statement (original) (raw)
Phillip J. Eby pje at telecommunity.com
Thu Jun 22 21:59:33 CEST 2006
- Previous message: [Python-Dev] Switch statement
- Next message: [Python-Dev] Switch statement
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
At 12:24 PM 6/22/2006 -0700, Guido van Rossum wrote:
OK, I think I see how this works. You pre-compute the expression at def-time, squirrel it away in a hidden field on the function object, and assign it to a local each time the statement is executed.
More precisely, I'd say that the computation is moved to function definition time and becomes an anonymous free variable. The body of the static expression becomes a LOAD_DEREF of the free variable, rather than computation of the expression.
The debug trace will show the function definition going to the lines that contain the static expressions, but that's understandable.
I think I like it. I was confused by what Fredrik meant by "const", but your renaming it to "static" makes more sense to me; i.e. it belongs to the function, as opposed to each execution of the function. (Whereas I was reading "const" as meaning "immutable" or "non-rebindable", which made no sense in the context.)
Unfortunately this would probably cause people to write
switch x: case static re.DOTALL: ... case static re.IGNORECASE: ... which is just more work to get the same effect as the def-time-switch-freezing proposal.
Without the "static", the reordering of execution isn't obvious. But perhaps that could be lived with, if the explanation was, "well, static is implied by case".
I'm also unclear on what you propose this would do without the statics. Would it be a compile-time error? Compile the dict each time the switch is executed? Degenerate to an if/elif chain? Then what if x is unhashable? What if some cases are static and others aren't?
If we allow non-static cases, then they should become "if"s that happen prior to a dictionary lookup on the remaining static/literal ones. Or we could just say that each adjacent block of static cases is its own dictionary lookup, and the rest happen in definition order. (i.e., you replace contiguous static/literal runs with dictionary lookups, and everything else is if-elif.)
- Previous message: [Python-Dev] Switch statement
- Next message: [Python-Dev] Switch statement
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]