[Python-Dev] Switch statement

Nick Coghlan ncoghlan at gmail.com
Sat Jun 24 06:29:34 CEST 2006

Guido van Rossum wrote:
> On 6/22/06, Nick Coghlan <ncoghlan at gmail.com> wrote:
>> Talin wrote:
>> > I don't get what the problem is here. A switch constant should have
>> > exactly the bahavior of a default value of a function parameter. We
>> > don't seem to have too many problems defining functions at the module
>> > level, do we?
>> Because in function definitions, if you put them inside another 
>> function, the
>> defaults of the inner function get reevaluated every time the outer 
>> function
>> is run. Doing that for the switch statement would kinda defeat the whole
>> point. . .
> Really? Then where would you store the dict? You can't store it on the
> code object because that's immutable. You can't store it on the
> function object (if you don't want it to be re-evaluated when the
> function is redefined) because a new function object is created by
> each redefinition. There needs to be *some* kind of object with a
> well-defined life cycle where to store the dict.
> I'd say that we should just add a warning against switches in nested
> functions that are called only once per definition.

I wasn't very clear. . .

Talin noted that there's no ambiguity with the timing of the evaluation of 
default function arguments, regardless of whether the function definition is 
at module scope or inside another function - the default arguments are simply 
evaluated every time the function definition is executed. So he wondered why 
that simplicity didn't translate to the evaluation of switch cases.

With a switch statement, we want the cases evaluated when the *containing* def 
statement is executed, not every time the switch statement itself is executed. 
Which makes things far more complex :)


Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia

More information about the Python-Dev mailing list