[Python-Dev] Simple Switch statement

Phillip J. Eby pje at telecommunity.com
Sun Jun 25 21:21:53 CEST 2006


At 11:13 AM 6/25/2006 -0700, Raymond Hettinger wrote:
>No thanks.  That is its own can of worms.  The obvious solutions (like const
>declarations, macros, or a syntax to force compile-time expression 
>evaluation)
>are unlikely to sit well because they run afoul Python's deeply ingrained
>dynamism.

I think perhaps you haven't been paying close attention to Fredrik's 
proposal.  The "static" operator simply lifts expression evaluation to 
function definition time, so that this:

     def x(y):
         print y * static(123+456)

becomes equivalent to this:

     foo = 123+456
     def x(y):
         print y * foo

This simple transformation doesn't "run afoul" of anything that I'm aware 
of, any more than the "with:" statement does.

Meanwhile, you seem to be arguing that forcing the use of literals at 
compilation time is somehow more dynamic than allowing them to be computed 
at runtime!  I don't get it.  Are you perhaps thinking that it's necessary 
to know the values at compilation time in order to compile the switch 
statement?  If so, note that the dictionary entries can be loaded by the 
code where the function is defined, and accessed as a free variable within 
the function body.  This is the same way that other "static" expressions 
would be implemented.

(On an unrelated note, I think that maybe rather than making "static" look 
like a function call, we should use a form that looks more like a generator 
expression, conditional, or yield expression, i.e. (static 123+456) instead 
of static(123+456), as this emphasizes its nature as an element of language 
syntax rather than making it look like a function call.)



More information about the Python-Dev mailing list