473,402 Members | 2,053 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Is switch option available in Python?

440 256MB
Hi,

I came across only "if-else,if-elif,nested if else " in some of the Python links,whether we have the similar to "switch" option in Python.

Thanks in advance
PSB
Mar 6 '07 #1
2 1408
bartonc
6,596 Expert 4TB
Hi,

I came across only "if-else,if-elif,nested if else " in some of the Python links,whether we have the similar to "switch" option in Python.

Thanks in advance
PSB
There's no switch, but it's eazy to build a lookup table:
Expand|Select|Wrap|Line Numbers
  1. def func1():
  2.     pass
  3.  
  4. def func2():
  5.     pass
  6.  
  7. funcList = (func1, func2)
  8.  
  9. switchIndex = 0
  10.  
  11. funcList(switchIndex)()
Mar 6 '07 #2
ghostdog74
511 Expert 256MB
just for information you can see this PEP
in Python, you can simulate switch using dictionaries, like what barton says a lookup table. eg
Expand|Select|Wrap|Line Numbers
  1. switch = {'option1': function1,
  2.      'option2': function2,
  3.      'option3': function3,
  4.      'option4': function4}
  5. if value in switch:
  6.      switch[value]()
  7. else:
  8.      pass
  9.  
Mar 7 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: Christian Seberino | last post by:
I know IPython is another interpreter for Python and was wondering what people liked about it and if I should switch to it. If it is so good then why is it not part of the standard Python...
14
by: Rudi Hansen | last post by:
I dont seem to be able to find the switch statement in Python. I would like to be able to do switch(var) case 1 : print "var = 1" case 2: print "var = 2"
6
by: Jeff Duffy | last post by:
Hi all. I've been wondering why python itself doesn't provide a switch to check a file for valid syntax. I know that you can currently call python -c "import py_compile;...
10
by: Evie | last post by:
I understand that when a switch statement is used without breaks, the code continues executing even after a matching case is found. Why, though, are subsequent cases not evaluated? I wrote a...
16
by: ME | last post by:
In C# the following code generates a compiler error ("A constant value is expected"): public void Test(string value) { switch (value) { case SimpleEnum.One.ToString(): MessageBox.Show("Test...
7
by: cytec123187 | last post by:
Hello, I am working on an Adobe Acrobat file that uses javascript for calculations. I am trying to create a field that uses two other fields to determine a number value. I think this requires...
8
by: Andrew Robert | last post by:
Hi Everyone. I tried the following to get input into optionparser from either a file or command line. The code below detects the passed file argument and prints the file contents but the...
10
by: neil | last post by:
Hello group, I have a question I hope someone knowledgeable here might assist me with :o) I am working on a python exporter to get a scene out of Blender and into a renderer called Indigo. I...
1
by: emp | last post by:
This post went around back in January. I need some clarification. I have an enum declaring 30 enumerated values a=0 b=1 c=2 etc I have a switch statement that I don't want to use all 30...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.