Posts: 4
Threads: 1
Joined: Nov 2017
Hello.
I wonder that in Python there's absent "switch" statement.
And the most interesting - nobody asks about it, nobody discusses it.
Seems, it's very often used in Javascript/Coffeescript, who have such statement.
But in Python it's absent.
There were added some less often-used features in Python, like decorators, "with" statements, "as" operator.
But no "switch" .
Please, tell me what do you think about this.
Maybe I don't know something and writing each time "if somevar == 'SOMEVALUE'" is a better practice ?
Thank you.
Best regards,
Vladislav
Posts: 8,090
Threads: 154
Joined: Sep 2016
In python we use dict for this , e.g something like this basic example:
def foo():
print('This is call from foo.')
def bar():
print('This is call from bar.')
my_switch = {'1':foo, '2':bar}
while True:
user_input = input('Select function to execute (1 for foo, 2 for bar): ' )
try:
my_switch[user_input]()
except KeyError:
print('This is not valid input')
break Output: Select function to execute (1 for foo, 2 for bar): 2
This is call from bar.
Select function to execute (1 for foo, 2 for bar): 1
This is call from foo.
Select function to execute (1 for foo, 2 for bar): 1
This is call from foo.
Select function to execute (1 for foo, 2 for bar): 2
This is call from bar.
Select function to execute (1 for foo, 2 for bar): 3
This is not valid input
Posts: 5,150
Threads: 396
Joined: Sep 2016
I always actually thought when doing c++ that switch was repetitive. I always just used the if condition there. But when i came to python, i loved the dictionary method. I never looked back.
Recommended Tutorials:
Posts: 2,955
Threads: 48
Joined: Sep 2016
Nov-18-2017, 07:39 PM
(This post was last modified: Nov-18-2017, 07:40 PM by wavic.)
In Python the equivalent of switch statement is:
if condition:
# do something
elif condition:
# do something else
elif condition:
# do another
Posts: 8,090
Threads: 154
Joined: Sep 2016
Nov-18-2017, 07:52 PM
(This post was last modified: Nov-18-2017, 07:52 PM by buran.)
@ wavic there is if/else also in javascript and virtually in any language and that is what OP doesn't want to do/ does not ask about it
Posts: 11,877
Threads: 474
Joined: Sep 2016
Nov-19-2017, 03:05 AM
(This post was last modified: Nov-19-2017, 03:06 AM by Larz60+.)
try this:
class PseudoSwitch:
def __init__(self):
self.exec_dict = {
'1': self.function1,
'2': self.function2,
'3': self.function3
}
def function1(self):
print('You ran function 1')
def function2(self):
print('You ran function 2')
def function3(self):
print('You ran function 3')
def tryit(self):
try:
choice = input('enter function number: ')
print(choice)
self.exec_dict[choice]()
except:
'function out of range, use 1 2 or 3'
def main():
ps = PseudoSwitch()
ps.tryit()
if __name__ == '__main__':
main()
Posts: 3,458
Threads: 101
Joined: Sep 2016
Nov-19-2017, 04:48 PM
(This post was last modified: Nov-19-2017, 04:49 PM by nilamo.)
Switch is just a pattern matching construct. Javascript/coffeescript have a pretty mediocre version of it, so in my opinion, Python lacking it at all is better than having a halfway useful feature.
Now, if it could match on ranges, conditional checks, the type of the param (is_instance), or the return value of a function, instead of just whatever specific value it has, then it'd be more useful. But as is, it is rarely useful even in javascript.
Posts: 470
Threads: 29
Joined: Sep 2016
Nov-20-2017, 11:57 AM
(This post was last modified: Nov-21-2017, 09:12 AM by Kebap.)
(Nov-18-2017, 01:35 PM)buran Wrote: my_switch = {'1':foo, '2':bar}
while True:
user_input = input('Select function to execute (1 for foo, 2 for bar): ' )
I would even go as far to have the options be calculated from the dictionary, so if you change the dict, the question will automatically mention the new function as an option as well, and you don't have to think about changing two places.
Posts: 8,090
Threads: 154
Joined: Sep 2016
(Nov-20-2017, 11:57 AM)Kebap Wrote: I would even go as far to have the options be calculated from the dictionary, so if you change the dict, the question will automatically mention the new function as an option as well, and you don't have to think about changing two places. I agree for production/real-life code, but this was meant as merely an example :-)
Posts: 4
Threads: 1
Joined: Nov 2017
Nov-22-2017, 10:30 PM
(This post was last modified: Nov-22-2017, 10:43 PM by vlad1777d.)
Thank you very much for this. It's as for me, very good idea =)
(Nov-19-2017, 03:05 AM)Larz60+ Wrote: try this:
class PseudoSwitch:
def __init__(self):
self.exec_dict = {
'1': self.function1,
'2': self.function2,
'3': self.function3
}
def function1(self):
print('You ran function 1')
def function2(self):
print('You ran function 2')
def function3(self):
print('You ran function 3')
def tryit(self):
try:
choice = input('enter function number: ')
print(choice)
self.exec_dict[choice]()
except:
'function out of range, use 1 2 or 3'
def main():
ps = PseudoSwitch()
ps.tryit()
if __name__ == '__main__':
main()
Thanks, it's a good idea =)
(Nov-19-2017, 04:48 PM)nilamo Wrote: Switch is just a pattern matching construct. Javascript/coffeescript have a pretty mediocre version of it, so in my opinion, Python lacking it at all is better than having a halfway useful feature.
Now, if it could match on ranges, conditional checks, the type of the param (is_instance), or the return value of a function, instead of just whatever specific value it has, then it'd be more useful. But as is, it is rarely useful even in javascript.
I don't know, I really often use it. Maybe as often, as if-else construction...
I wonder, why do lesser often used syntax features were added (decorators, with, as) to language; though they could be expressed as you just expressed "switch" statement - through current language features.
Decorators - through functions, as - through "=", with - through classes or functions.
|