Since I started developing with python I never found myself in the need of such statement. As I remember I never asked where is the switch statement in this language. I think I never asked that because of the dictionary data type in python that you can use to do the same thing as a switch statement will do. I was surprised that there are many PEPs about adding the switch statement to python.
In other languages the switch statement looks like this:
in python one can do the same thing with the help of a dictionary like this:
Here is link to a post where there are many comments that try to come up with an alternative:
http://simonwillison.net/2004/May/7/switch/
Why should python have a switch statement when there is a more powerful way to do the same with a dict data type ?
Should Python also have a GOTO ?
In other languages the switch statement looks like this:
switch (var)
{
case value1: do_some_stuff1();
case value2: do_some_stuff2();
...
case valueN: do_some_stuffN();
default: do_default_stuff();
}
in python one can do the same thing with the help of a dictionary like this:
values = {
value1: do_some_stuff1,
value2: do_some_stuff2,
...
valueN: do_some_stuffN,
}
values.get(var, do_default_stuff)()
Here is link to a post where there are many comments that try to come up with an alternative:
http://simonwillison.net/2004/May/7/switch/
Why should python have a switch statement when there is a more powerful way to do the same with a dict data type ?
Should Python also have a GOTO ?
31 Jul 2007 23:02:36
Well, for one thing lambdas can't seem to print. For example this doesn't work:
result = {
1 : lambda : print "value is 1",
2 : lambda : print "value is 2"
}
result.get(2, lambda: print "nothing")
Switch statements are far more flexible than the dictionary type for executing arbitrary code.
01 Aug 2007 19:25:17
You'r right. lambda cannot print because:
1. lambda cannot contain statements: http://docs.python.org/ref/...
2. if you want to print with lambda: http://www.p-nand-q.com/pyt...
To make the example you wrote work, remove "print" from all lambda and put a "print" before result.get
17 Mar 2008 16:52:56
What about different function signatures? How would you translate the following, for example?
switch( var )
case1: printStr( 'Hello!' );
case2: printError();
06 Apr 2008 01:13:55
A large part of the power of switch statements is that a matching case is run AND so is every case after the matching case until a break statement is encountered. Additionally, switch statements have a default case which runs if there was no matching case or if there was no break after a matching case. The lambda hacks miss these useful features. I think PHP's implementation of switch is quite nice.
29 Jan 2009 22:03:05
I know that this was posted forever ago, but my answer to "Why should python have a switch statement when there is a more powerful way to do the same with a dict data type?" is to ask another question:
"Why should python have an IF statement when there is a more powerful way to do the same with a dict data type?"
def foo():
print "foo"
def bar():
print "bar"
values = {
True: foo,
False: bar
}
>>> values.get(1==1)()
foo
>>> values.get(1!=1)()
bar
Orthogonality of your language *is* arguably a desirable quality to have, but only to a point. I think you'd be hard-pressed to find someone to argue the above is correct. What makes switch different?
08 Apr 2009 20:51:48
Python should never have a GOTO. Definitely.
18 Apr 2009 14:58:51
I'm looking for a technique that will let me make case/switch ASSIGNMENTS on the fly; eg.
case(x = "red"):
name = "apple"
case(x = "blue"):
name = "blueberry"
case(x = "pink"):
name = "...watermellon?"
return "Your fruit is",name<ctrl-s>[habit:)]
Anyway, my point: I can bind dictionary- and lambda-embedded functions to COMPUTATIONS(eg. if x = 1, return 2*3), but not to ASSIGNMENTS (eg. if x = 1, name = "red").
Suggestions?
25 May 2009 23:46:25
John,
fruits = {
"red": lamdba: "apple",
"blue": lambda: "blueberry",
"pink": lambda: "watermelon"
}
name = fruits.get("red")()
print "Your fruit is " + name
You can also do the same thing by changing (for instance) this
lambda: "apple"
into this
do_apple()
and then making a function like this
do_apple():
global name
name = "apple"
return apple
(the 'return apple' is optional).
This problem isn't python specific, it's more a generic problem of scope which you'll encounter in any language that cleans up function variables. Even C won't let you call a local variable globally unless you tell it to.
13 Dec 2009 04:53:35
here is the reason why they need to add a switch statement: that does not work, (or atleast it didn't for me), and it's confusing, they are supposed to be variables, not commands, I honestly don't see how that could possibly have run the code in the condition parts
13 Dec 2009 04:54:23
what's lambda?
03 Jan 2010 00:55:34
John,
Just replace 'case' in your code with 'if'.
09 Jan 2010 07:03:52
Well, for one thing lambdas can't seem to print. For example this doesn't work:
result = {
1 : lambda : print "value is 1",
2 : lambda : print "value is 2"
}
result.get(2, lambda: print "nothing")
Switch statements are far more flexible than the dictionary type for executing arbitrary code.