[Python-Dev] Switch statement

Fredrik Lundh fredrik at pythonware.com
Thu Jun 22 20:21:41 CEST 2006

Guido van Rossum wrote:

>>      def foo(value):
>>         const bar = fie.fum
>>         if value == bar:
>>            ...
>> which would behave like
>>      def foo(value, bar=fie.fum):
>>         if value == bar:
>>             ...
>> but without the "what if we pass in more than one argument?" issue.
> So the constant would be evaluated at function definition time? I find
> that rather confusing.

well, I find the proposed magic behaviour of "case" at least as confusing...

>> (except for the default argument thing; see above).  the result is a
>> kind of semi-constant objects that would be useful, but perhaps not
>> constant enough...)
> I fail to see the usefulness of this wrapper. The wrapper isn't
> completely transparent o some code that uses type checks may need to
> be modified. The wrapper doesn't get removed by a simple assignment;
> after
>   const a = 1
>   b = a
> how do we prevent b from being treated as a constant?

we cannot -- this approaches assigns (a small amount of) const-ness to 
objects, not names.

>> it might be too much C# exposure, but I think I prefer the "explicit
>> when using" approach...
> It may be not enough C# exposure, but I don't know exactly which
> approach you are referring to.

the original one: if you want to treat an expression as a constant, you 
have to be explicit.  examples:

>>> a "constant" (or perhaps better, "const") primary would also be useful
>>> in several other cases, including:
>>> - as a replacement for default-argument object binding

this is used when you want to pass an *object* into an inner function, 
rather than a name:

     def foo(value, bar=fie.fum):
         if value == bar:

can be written

     def foo(value):
         if value == const bar:

>>> - local dispatch tables, and other generated-but-static data structures

     def foo(value):
         table = const {
             1: "one",
             2: "two",
             3: fie.fum,

(maybe "static" would be a better keyword?)

>>> - explicit (but still anonymous) constant/expression "folding"

     def foo(value):
         if value < const (math.pi / 2):

and so on.  to implement this, the runtime simply evaluates the "const" 
expressions together with the default value expressions, and assigns the 
result to some func_xxx attribute.  everything else works as usual.


More information about the Python-Dev mailing list