[Python-3000] Switch and static, redux

Greg Ewing greg.ewing at canterbury.ac.nz
Thu Jul 6 02:24:19 CEST 2006


Guido van Rossum wrote:

> So, my proposal is to give up on static, accept PEP 3103 with the
> following options:
>   - Syntax alternative 2+B (unindented cases, 'case in ...' for multiple cases).
>   - Semantics option 3 (def-time freezing)

I agree with giving up on static, or at least treating
it as a separate issue from switch.

But there's one more thing I'd like to see discussed
concerning def-time freezing:

    apple = 1
    banana = 2

    def f():
      ...
      def g(fruit):
        ...
        switch fruit:
          case apple:
            ...
          case banana:
            ...

With def-time freezing as currently proposed, this will
evaluate the cases inside g() every time f() is called.
But that would be wasteful, because they only depend on
names defined at the module level.

So I'd like to propose that "def time" be modified
slightly to mean "def time of the outermost function
that is still inside the scopes containing all the
names that the case expressions depend on". Not
sure about the wording of that, but I hope you get the
idea. In the above example, that would be the def time
of f(), rather than g().

I'd further suggest that a switch be allowed to reference
names in the current scope, in which case no optimisation
is done. That would take care of switches at the module
level, without making the module level a special case
with respect to the evaluation time rules.

--
Greg


More information about the Python-3000 mailing list