[Python-Dev] Switch statement

Guido van Rossum guido at python.org
Thu Jun 22 20:52:59 CEST 2006


On 6/22/06, Phillip J. Eby <pje at telecommunity.com> wrote:
> I think one of the problems I sometimes have in communicating with you is
> that I think out stuff from top to bottom of an email, and sometimes
> discard working assumptions once they're no longer needed.  We then end up
> having arguments over ideas I already discarded, because you find the
> problems with them faster than I do, and you assume that those problems
> carry through to the end of my message.  :)

You *do* have a text editor that lets you go back to the top of the
draft to remove discarded ideas, don't you? :-)

It's a reasonable form of discourse to propose an idea only to shoot
it down, but usually this is introduced by some phrase that hints to
the reader what's going to happen. You can't expect the reader to read
the entire email before turning on their brain. :)

> So, I'm partially reversing
> the order of my reply, so you can see what I'm actually proposing, before
> the minutiae of responding the objections you raised to stuff I threw out
> either in my previous message or the message before that.   Hopefully this
> will help.
>
> At 10:44 AM 6/22/2006 -0700, Guido van Rossum wrote:
> >Please, think through the notion of const declarations more before
> >posting again. Without const declarations none of this can work
>
> Actually, the "const" declaration part isn't necessary and I already
> discarded the idea in my  previous reply to you, noting that the
> combination of these facets can be made to work without any explicit const
> declarations:
>
> 1. "case (literal|NAME)" is the syntax for equality testing -- you can't
> use an arbitrary expression, not even a dotted name.

But dotted names are important! E.g. case re.DOTALL. And sometimes
compile-time constant expressions are too. Example: case sys.maxint-1.

> 2. NAME, if used, must be bound at most once in its defining scope

That's fine -- but doesn't extend to dotted names.

> 3. Dictionary optimization can occur only for literals and names not bound
> in the local scope, others must use if-then.

So this wouldn't be optimized?!

NL = "\n"
for line in sys.stdin:
  switch line:
    "abc\n": ...
    NL: ...

> This doesn't require explicit "const" declarations at all.  It does,
> however, prohibit using "import A" and then switching on a bunch of "A.foo"
> values.  You have to "from A import foo, bar, baz" instead.
>
> If you like this, then you may not need to read the rest of this message,
> because most of your remaining comments and questions were based on an
> assumption that "const" declarations were necessary.

I like it better than const declarations, but I don't like it as much
as the def-time-switch-freezing proposal; I find the limitiation to
simple literals and names too restrictive, and there isn't anything
else like that in Python. I also don't like the possibility that it
degenerates to if/elif. I like predictability. I like to be able to
switch on dotted names.

Also, when using a set in a case, one should be able to use an
expression like s1|s2 in a case.

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


More information about the Python-Dev mailing list