[Python-Dev] Switch statement

Fredrik Lundh fredrik at pythonware.com
Thu Jun 22 18:22:17 CEST 2006


Guido van Rossum wrote:
>> which simply means that expr will be evaluated at function definition
>> time, rather than at runtime. example usage:
>>>> var = expression
>> if var == constant sre.FOO:
>> ...
>> elif var == constant sre.BAR:
>> ...
>> elif var in constant (sre.FIE, sre.FUM):
>> ...
>> This gets pretty repetitive. One might suggest that 'case' could imply
> 'constant'...?

possibly, but I find that a tad too magic for my taste.
a "constant" (or perhaps better, "const") primary would also be useful 
in several other cases, including:
- as a replacement for default-argument object binding
- local dispatch tables, and other generated-but-static data structures
- explicit (but still anonymous) constant/expression "folding"
an alternative would be to add a const declaration that can only be used 
in local scopes; i.e.
 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.
yet another alternative would be a const declaration that you could use 
on a global level, but I fail to see how you could propagate the "const- 
ness" property to whoever wants to use a const object -- unless, of 
course, you implement
 const bar = fie.fum
 def foo(value):
 if value == bar:
 ...
as
 class constant_wrapper(object):
 def __init__(self, value):
 self.value = value
 bar = constant_wrapper(fie.fum)
 def foo(value, bar=bar.value):
 if value == bar:
 ...
(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...)
it might be too much C# exposure, but I think I prefer the "explicit 
when using" approach...
</F>


More information about the Python-Dev mailing list

AltStyle によって変換されたページ (->オリジナル) /