[Python-Dev] Simple Switch statement (original) (raw)
Guido van Rossum guido at python.org
Sun Jun 25 18:16:19 CEST 2006
- Previous message: [Python-Dev] Simple Switch statement
- Next message: [Python-Dev] Simple Switch statement
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Sorry, no go. You can say "supports key use cases found in real code" as often as you like, but limiting it only to literals drops many use cases on the floor, and is unacceptable for me. There are no other places in Python where only literals are allowed. In your eagerness to rule out surprises, you're creating the biggest surprise of all: the restriction to literals is certainly a surprise!
If you want to provide a solution for the constantification issue, let's discuss that first and then come back here.
--Guido
On 6/24/06, Raymond Hettinger <raymond.hettinger at verizon.net> wrote:
From what I can see, almost everyone wants a switch statement, though perhaps for different reasons.
The main points of contention are 1) a non-ambiguous syntax for assigning multiple cases to a single block of code, 2) how to compile variables as constants in a case statement, and 3) handling overlapping cases. Here's a simple approach that will provide most of the benefit without trying to overdo it: switch f(x): # any expression is allowable here but raises an exception if the result is not hashable case 1: g() # matches when f(x)==1 case 2,3 : h() # matches when f(x) in (2,3) case 1: i() # won't ever match because the first case 1 wins case (4,5), 6: j() # matches when f(x) in ((4,5), 6) case "bingo": k() # matches when f(x) in ("bingo",) default: l() # matches if nothing else does Though implemented as a hash table, this would execute as if written: fx = f(x) hash(fx) if fx in (1,): g() elif fx in (2,3): h() elif fx in (1,): i() elif fx in ((4,5), 6): j() elif fx in ("bingo",): k() else: l() [...]
-- --Guido van Rossum (home page: http://www.python.org/~guido/)
- Previous message: [Python-Dev] Simple Switch statement
- Next message: [Python-Dev] Simple Switch statement
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]