![]() |
|
#1
|
||||
|
||||
Making a "switch" functionFor those who don't know, Python does not have a switch() function, unlike many other languages. Instead you have to use if/elif statements, like so:
Python Code:
However, I just stumbled upon a site (http://www.voidspace.org.uk/python/articles/OOP.shtml) that gives a nifty way of making a switch() style function using dictionaries. It's slightly more complicated than using if/elif statements, but it may make more sense if you have a lot of options. Which way is better is up to you to decide. Python Code:
__________________
Start Programming with Python-A beginner's guide to programming and the Python language. ------------- Common Sense v2.0-Striving to make the world a little bit smarter. |
||||
|
#2
|
||||
|
||||
Re: Making a "switch" functionI had somewhat similar problems in C++ with string elements : you can't use a switch statement on strings! When configuring an application with XML files, making programmatic decisions based on string comparison can make sense.
The solution was to use maps. I had to add each string element to the map, in a similar way you use the dictionary. Tying the string to an action is more complicated however, and even without such a requirement, the Python code looks much nicer! Thanks, Lucian __________________
Please read these Guidelines before posting on the forum "A person who never made a mistake never tried anything new." Einstein |
|
#3
|
||||
|
||||
Re: Making a "switch" functionI remember in my C++ class having a problem that had me trying to use strings with a switch(). I didn't know about maps at the time so I kludged a similar way to do it by using integers as the switch() argument, then tying the number to a string.
It probably wasn't as "elegant" as a map, but I agree that the Python way is easier. __________________
Start Programming with Python-A beginner's guide to programming and the Python language. ------------- Common Sense v2.0-Striving to make the world a little bit smarter. |
|
#4
|
|||
|
|||
Re: Making a "switch" functionQuote:
|
|
#5
|
||||
|
||||
Re: Making a "switch" functionThe if/elif construct is actually really easy to use and it makes sense. I've never tried the above switch dictionary because it's a lot more work to get the same thing.
I wasn't aware of the speed difference in C++ though. How bad can the performance hit be, assuming worst case scenarios? __________________
Start Programming with Python-A beginner's guide to programming and the Python language. ------------- Common Sense v2.0-Striving to make the world a little bit smarter. |
Recent GIDBlog
Not selected for officer school by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| [Include] Doubly-linked List | dsmith | C Programming Language | 6 | 14-Apr-2006 14:12 |
| [Tutorial] Function Pointers | aaroncohn | C++ Forum | 4 | 17-Feb-2006 12:33 |
| [GIM] gim.h | dsmith | C Programming Language | 0 | 18-Jan-2005 09:48 |
| Help making a function | darkzerox | C Programming Language | 4 | 23-May-2004 14:55 |
| Revising Script style ?????? | pepee | MySQL / PHP Forum | 4 | 14-Apr-2004 05:59 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The