[Python-Dev] Switch statement

Fredrik Lundh fredrik at pythonware.com
Wed Jun 21 19:53:42 CEST 2006


Guido van Rossum wrote:

> 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.

unless we come up with a way to make it possible to mark an variable as 
a constant.

> The proposed switch semantics (create the table when the containing
> function is defined) get around this by "defining" what it means by
> "constant".

well, given that people find it really confusing that the two X:es in

    def func(value=X):
        print X

are evaluated at different times, I'm not sure it's a good idea to 
introduce more "evaluation scopes".

but sure, I'm sure people doing certification tests would love questions 
like:

     Q: If a program calls the 'func' function below as 'func()'
        and ONE and TWO are both integer objects, what does 'func'
        print ?

     ONE = 1
     TWO = 2

     def func(value=ONE):
         switch value:
         case ONE:
             print value, "is", ONE
         case TWO:
             print value, "is", TWO

     a: "1 is 1"
     b: "2 is 2"
     c: nothing at all
     d: either "1 is 1" or nothing at all
     e: who knows ?

but I cannot say I find it especially Pythonic, really...

</F>



More information about the Python-Dev mailing list