[Python-Dev] Switch statement
Guido van Rossum
guido at python.org
Fri Jun 23 02:12:29 CEST 2006
On 6/22/06, Phillip J. Eby <pje at telecommunity.com> wrote:
> At 12:54 PM 6/22/2006 -0700, Guido van Rossum wrote:
> >Summarizing our disagreement, I think you feel that
> >freeze-on-first-use is most easily explained and understood while I
> >feel that freeze-at-def-time is more robust. I'm not sure how to get
> >past this point except by stating that you haven't convinced me... I
> >think it's time to sit back and wait for someone else to weigh in with
> >a new argument.
> Which I think you and Fredrik have found, if "case" implies "static". It
> also looks attractive as an addition in its own right, independent of "switch".
> In any case, my point wasn't to convince you but to make you aware of
> certain costs and benefits that I wasn't sure you'd perceived. It's clear
> from your response that you *have* perceived them now, so I'm quite
> satisfied by that outcome -- i.e., my goal wasn't to "convince" you to
> adopt a particular proposal, but rather to make sure you understood and
> considered the ramifications of the ones under discussion.
> That being said, there isn't anything to "get past"; from my POV, the
> discussion is already a success. :)
That sounds like a good solution all around. I hope that others can
also find themselves in this.
(1) An expression of the form 'static' <atom> has the semantics of
evaluating the atom at the same time as the nearest surrounding
function definition. If there is no surrounding function definition,
'static' is a no-op and the expression is evaluated every time.
[Alternative 1: this is an error] [Alternative 2: it is evaluated
before the module is entered; this would imply it can not involve any
imported names but it can involve builtins] [Alternative 3:
precomputed the first time the switch is entered]
(2) All case expressions in a switch have an implied 'static'.
(3) A switch is implemented using a dict which is precomputed at the
same time its static expressions are precomputed. The switch
expression must be hashable. Overlap between different cases will
raise an exception at precomputation time.
Independent from this, I wonder if we also need static names of the form
static <name> = <expression>
which would be similar to
<name> = static (<expression>)
but also prevents <name> from being assigned to elsewhere in the same scope.
Also, I haven't heard a lot of thumbs up or down on the idea of using
to indicate a single value and
case in S:
to indicate a sequence of values.
(I'm not counting all the hypergeneralizations that were proposed like
case == X:
case < X:
case is X:
case isinstance X:
since I'm -1 on all those, no matter how nicely they align.
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-Dev