[Python-Dev] Switch statement

Talin talin at acm.org
Sat Jun 24 06:36:47 CEST 2006


Guido van Rossum wrote:

> That sounds like a good solution all around. I hope that others can
> also find themselves in this.
> 
> (1) An expression of the form 'static' <atom> has the semantics of
> evaluating the atom at the same time as the nearest surrounding
> function definition. If there is no surrounding function definition,
> 'static' is a no-op and the expression is evaluated every time.
> [Alternative 1: this is an error] [Alternative 2: it is evaluated
> before the module is entered; this would imply it can not involve any
> imported names but it can involve builtins] [Alternative 3:
> precomputed the first time the switch is entered]

I'm thinking that outside of a function, 'static' just means that the 
expression is evaluated at compile-time, with whatever symbols the 
compiler has access to (including any previously-defined statics in that 
module). The result of the expression is then inserted into the module 
code just like any literal.

So for example:

    a = static len( "1234" )

compiles as:

    a = 4

...assuming that you can call 'len' at compile time.

The rationale here is that I'm trying to create an analogy between 
functions and modules, where the 'static' declaration has an analogous 
relationship to a module as it does to a function. Since a module is 
'defined' when its code is compiled, that would be when the evaluation 
occurs.

I'm tempted to propose a way for the compiler to import static 
definitions from outside the module ('static import'?) however I 
recognize that this would greatly increase the fragility of Python, 
since now you have the possibility that a module could be compiled with 
a set of numeric constants that are out of date with respect to some 
other module.

-- Talin


More information about the Python-Dev mailing list