[Python-Dev] Switch statement
Guido van Rossum
guido at python.org
Wed Jun 21 19:27:23 CEST 2006
On 6/21/06, Phillip J. Eby <pje at telecommunity.com> wrote:
> At 09:55 AM 6/21/2006 -0700, Guido van Rossum wrote:
> >BTW a switch in a class should be treated the same as a global switch.
> >But what about a switch in a class in a function?
>
> Okay, now my head hurts. :)
Welcome to the club. There's a Monty Python sketch appropriate...
> A switch in a class doesn't need to be treated the same as a global switch,
> because locals()!=globals() in that case.
But that's not the discerning rule in my mind; the rule is, how to
define "at function definition time".
> I think the top-level is the only thing that really needs a special case
> vs. the general "error if you use a local variable in the expression" rule.
To the contrary, at the top level my preferred semantics don't care
because they don't use a hash.
The strict rules about locals apply when it occurs inside a function,
since then we eval the case expressions at function definition time,
when the locals are undefined. This would normally be good enough, but
I worry (a bit) about this case:
y = 12
def foo(x, y):
switch x:
case y: print "something"
which to the untrained observer (I care about untrained readers much
more than about untrained writers!) looks like it would print
something if x equals y, the argument, while in fact it prints
something if x equals 12.
> Actually, it might be simpler just to always reject local variables -- even
> at the top-level -- and be done with it.
Can't because locals at the top-level are also globals.
--
--Guido van Rossum (home page: http://www.python.org/~guido/)
More information about the Python-Dev
mailing list