[Python-Dev] Switch statement

Guido van Rossum guido at python.org
Thu Jun 22 18:45:18 CEST 2006


On 6/22/06, Fredrik Lundh <fredrik at pythonware.com> wrote:
> 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.

So the constant would be evaluated at function definition time? I find
that rather confusing. Especially since common uses will probably
include

  const true = True
  while true:
    ...code...

This is a well-meaning attempt to let the compiler optimize this to a
test-less infinite loop that works but throws the baby out with the
bathwater.

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

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?

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

-- 
--Guido van Rossum (home page: http://www.python.org/~guido/)


More information about the Python-Dev mailing list